how to write case stament if field exist then return same field other wise create require field

  • hi Friends i have small doubt in sql server

    how to write case statement if field exist then return same field other wise create field.that means i show table data like

    id,name,sal

    1 ,rav ,300

    2,ven ,900

    output

    id,name,sal,deptno

    1,rav ,300 , null

    2,ven ,900 ,null

    i tried like select id,name,sal,'null' as deptno from table.

    but its not good way because of some table dont have id fields and name fields,and some table having 100 fields so here we need check that field exist or not which ever required field not there then create that field othere wise return same field.

    can any body tell me how to write case statement in sql server

  • You can look up plenty of online example of CASE statements. To see what columns a table has, look in sys.columns.

    select name from MyDatabase.sys.columns where OBJECT_NAME(object_id) = 'MyTable'

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply