November 14, 2008 at 5:13 am
Hi All,
I have written stored procedure, based on selections I need to check values from the table. Here I will get tablename from the query and I need to pass the tablename in another query to fetch the values. When ever I pass the tablename in the if loop its giving error like incorrect syntax error near select. Please suggest
declare @Tablename varchar(30)
declare @fieldname varchar(20)
declare @fieldvalue varchar(10)
set @Tablename='t_trans_auth'
set @Fieldname='Payeeid'
set @fieldvalue=1
begin
if not exists('select'+@Fieldname +' from '+@TableName+ ' where '+ @Fieldname+'='+ @Fieldvalue)
if (@@rowcount=0)
Begin
Raiserror (65537,11,1)
return -1
End
end
Please advise.
Cheers,
Nandy
November 14, 2008 at 5:18 am
Nandy (11/14/2008)
Hi All,I have written stored procedure, based on selections I need to check values from the table. Here I will get tablename from the query and I need to pass the tablename in another query to fetch the values. When ever I pass the tablename in the if loop its giving error like incorrect syntax error near select. Please suggest
declare @Tablename varchar(30)
declare @fieldname varchar(20)
declare @fieldvalue varchar(10)
set @Tablename='t_trans_auth'
set @Fieldname='Payeeid'
set @fieldvalue=1
begin
if not exists('select'+@Fieldname +' from '+@TableName+ ' where '+ @Fieldname+'='+ @Fieldvalue)
if (@@rowcount=0)
Begin
Raiserror (65537,11,1)
return -1
End
end
Please advise.
Cheers,
Nandy
IF NOT EXISTS (SELECT * FROM tablename where employeeid = @EmployeeId)
BEGIN
UPDATE
SET LimitAccessByClubs = 0
WHERE = @EmployeeId
END
kshitij kumar
kshitij@krayknot.com
www.krayknot.com
November 14, 2008 at 6:18 am
you can't use a variable for an objectname..so you cannot stick something in to replace a tablename or fieldname directly in a statement...you have to use dynamic sql:
fails:
if not exists('select'+@Fieldname +' from '+@TableName+ ' where '+ @Fieldname+'='+ @Fieldvalue)
works:
exec('select ' + @Fieldname + ' from ' + @TableName+ ' where ' + @Fieldname + ' = ' + @Fieldvalue)
if @@rowcount.......
Lowell
November 14, 2008 at 12:47 pm
You can use the dynamic SQL for this, and make sure you have turned on the set count on. and use @@RowCount to check the record count of last executed query.
November 16, 2008 at 9:35 pm
Hi All,
I have used the dynamic SQL and executed, and I used rowcount to check whether the SQL returning the value or not.
Many thanks for your help.
Nandy
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply