sql server - Checking if an id exist in another table -


i have customer table , order table , i'm trying check make sure customer in order table exists in customer table before i'm allowed execute rest of script.

this code that's causing problem,

    alter proc order   @customerid int   declare @cusid int   select @cusid = dbo.customertable.customerid  dbo.customertable inner join dbo.orderstable on dbo.customertable.customerid = dbo.orderstable.customerid  dbo.orderstable.customerid = @customerid  if(@cusid != @customerid)  begin raiserror('the customer not exist') return 1 end   exec order 44 

when try execute script customerid that's not in table doesn't give me error message. appreciated.

alter procedure dbo.[order]  --<-- avoid using key words object names @customerid int begin   set nocount on; if not exists             (select 1             dbo.customertable inner join dbo.orderstable             on dbo.customertable.customerid = dbo.orderstable.customerid             dbo.orderstable.customerid = @customerid)  begin    raiserror('the customer not exist', 16,1)    return 1  end     return @customerid; end 

execute proc

since procedure returns value need execute little bit differently,

declare @rtnvalue int;  execute @rtnvalue =  dbo.[order] 44  select @rtnvalue;    --<-- can use value whatever want 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -