SQL Help

  • 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.

  • Try this:

    DECLARE @sql VARCHAR(MAX)

    SELECT

    @sql = CASE

    WHEN x = 1 THEN 'update...'

    WHEN x = 2 THEN 'update...'

    END

    FROM myTable

    EXEC (@SQL)

  • ---

  • 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

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • 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.

  • 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