| Author |
Topic |
|
ibin
Starting Member
26 Posts |
Posted - 2009-10-13 : 06:03:18
|
| hi i am trying to check if a column exists and then select that statement using the code below. but it executes the select statement irrespective of whether it exists or not and gives an error message saying invalid column nameif Exists(select * from information_schema.columns where table_Name = N'Temp' and column_name like N'test%') beginSelect testcol from Tempendlet me know if iam missing anything here |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-10-13 : 06:06:40
|
| if Exists(select * from information_schema.columns where table_Name = N'Temp' and column_name = N'testcol')beginSelect testcol from TempendSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
sanoj_av
Posting Yak Master
118 Posts |
Posted - 2009-10-13 : 06:07:08
|
| why do you check with like operator? The column name can be something that starts with test but not the exact name testcol. |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-10-13 : 06:10:06
|
Do you have another column with test<smthg> as the name?You could see by doing: if Exists(select 1 from information_schema.columns where table_Name = N'Temp' and column_name like N'test%') beginselect 'got another column with name test something'end Another way around this is to use:if Exists(select 1 from information_schema.columns where table_Name = N'Temp' and column_name like N'test%') beginexec sp_executesql N'Select testcol from Temp'end Then it will not precompile the statement. |
 |
|
|
ibin
Starting Member
26 Posts |
Posted - 2009-10-13 : 06:35:51
|
| Thanks Rick...for ur reply.. it works...but for the statement like below throws incorrect syntax..if Exists(select * from information_schema.columns where table_Name = N'Temp' and column_name = N'test')beginexec sp_executesql N'Select [test] as ID ,'' As test1 from Temp where [test] like 'test'' endwht is the actual syntax?? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-13 : 06:39:44
|
| exec sp_executesql N'Select [test] as ID ,'''' As test1 from Temp where [test] like ''test''%'MadhivananFailing to plan is Planning to fail |
 |
|
|
ibin
Starting Member
26 Posts |
Posted - 2009-10-13 : 06:45:29
|
| Thanks all..... it got resolved...!! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-13 : 06:54:59
|
| Also why do you need dynamic sql?MadhivananFailing to plan is Planning to fail |
 |
|
|
ibin
Starting Member
26 Posts |
Posted - 2009-10-13 : 08:15:25
|
| for the above stated requirement.. is there any other means...??do let me know. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-13 : 08:27:40
|
| if Exists(select * from information_schema.columns where table_Name = N'Temp' and column_name = N'test')beginSelect [test] as ID ,'' As test1 from Temp where [test] like 'test'endMadhivananFailing to plan is Planning to fail |
 |
|
|
ibin
Starting Member
26 Posts |
Posted - 2009-10-13 : 08:43:56
|
| thanks.. i tried this but it was still compiling the select statement inside and gave an invalid column error.and just to avoid that i opted for dynamic sql. Now there is one more issue. actually my query is like thisupdate tableset col1 = testfrom table join(select col1 as col, col2 as test from tbl where bla bla blaunion select testcol as col, testcol2 as test from tbl where ---)ckl on -----continiues..here there are possibility tht i dont have the columns which i am trying to access so wanted to use the if condition. But now can we use the if exists statement between union..?? |
 |
|
|
|