October 4, 2010 at 1:10 am
Respected,
I write a query
declare @tablename varchar(128)
set @tablename='MyDatabase.CDC.dbo.TK_Member_CDC'
Select Case When Operation = 1 Then Update'+@tablename+
'Set [sb_Record_status] ="'+convert(varchar(2),1)+
'where id='++convert(varchar(20),[sb_id])
when 2 then 'Insert into '+@tablename+
'(Sb_ID,
sb_username,
sb_password,
sb_firstname
) values ('+
convert(varchar(20),sb_Id)+', '''+
IsNull([Sb_username],'Null')+''''+', '''+
IsNull([sb_password],'Null')+''''+', '''+
IsNull([sb_firstname],'Null')+'''')'
End
From mytable
this query execute fine. I need to also execute update and insert statement which are created in select statement.
Need Urgent Help
Regards & Thanks in Advance.
Naseer.
October 4, 2010 at 2:43 am
Try this:
DECLARE @sql VARCHAR(MAX)
SELECT
@sql = CASE
WHEN x = 1 THEN 'update...'
WHEN x = 2 THEN 'update...'
END
FROM myTable
EXEC (@SQL)
October 4, 2010 at 6:36 am
---
October 4, 2010 at 7:05 am
Two words:
SQL Injection
Make sure you're familiar with the SQL injection vulnerability and that you've taken appropriate steps to prevent hacks from occurring, because this procedure is vulnerable.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
October 4, 2010 at 7:21 am
Also, dont know if this will even apply to your use case without knowing more, but you might benefit by familiarizing yourself with the MERGE syntax; great for upsert logic.
October 5, 2010 at 1:57 am
Thanks for reply.
It not work for me . I tried to done with Merge Statement. can I Use CASE Statement and IF-ELSE in Merge Statement.
Regards & Thanks in Advance,
Naseer Ahmed
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply