Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ranjeetsingh_6
Posting Yak Master
125 Posts |
Posted - 2007-03-09 : 01:52:19
|
Hii want to check that Table Exis or not how it will done.In my stored procedure i pass Tablename by parameter.my main problem is in EXISTS() i want to pass Tablename by variable.IF EXISTS(select * from @user_id)begindeclare @s1 varchar(100)set @s1='DROP table' +' '+ @user_idexec(@s1)Ranjeet Kumar Singh |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-09 : 01:54:54
|
[code]If exists(select * from information_schema.tables where table_name = @tableName and Table_Type = 'BASE TABLE')...[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
ranjeetsingh_6
Posting Yak Master
125 Posts |
Posted - 2007-03-09 : 02:05:21
|
ThanksHow this task will done by Object_id() function i am using like thisif Object_id(@tablename) is not nullbegindeclare @s1 varchar(100) set @s1='DROP table' +' '+ @tablenameexec(@s1)endRanjeet Kumar Singh |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-09 : 02:17:26
|
That will work too. So what problem you are facing?Note: From Object ID, you can't be sure whether it is table, stored proc, UDF, view etc.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
ranjeetsingh_6
Posting Yak Master
125 Posts |
Posted - 2007-03-09 : 02:24:55
|
My procedure run sucessfully but does not return outputThis is below create procedure record@user_id varchar(20)=null,@frtime varchar(100)=null,@totime varchar(100)=nullasSET NOCOUNT ondeclare @query varchar(2500),@exist varchar(100)declare @tablename varchar(100),@yes varchar(10)If exists(select * from information_schema.tables where table_name = @user_id and Table_Type = 'BASE TABLE')beginset @yes='yes'set @tablename='DROP table' +' '+ @user_idexec(@tablename)select @query='select distinct tempaddplace.* into'+' '+ @user_id +' '+ ' from JOURNEY_REGT_DET inner join assign_vehicle_access on JOURNEY_REGT_DET.vehicle_id=assign_vehicle_access.vehicle_id inner join tempaddplace ontempaddplace.Jrid=JOURNEY_REGT_DET.JOURNEY_ID where assign_vehicle_access.Assign ='''+@yes+''' and assign_vehicle_access.user_id = ''' + @user_id + ''' and tempaddplace.Timerecorded between Convert(datetime,'''+@frtime+''',103) and Convert(datetime,'''+@totime+''',103)'exec(@tablename)endelsebeginselect @query='select distinct tempaddplace.* into'+' '+ @user_id +' '+ ' from JOURNEY_REGT_DET inner join assign_vehicle_access on JOURNEY_REGT_DET.vehicle_id=assign_vehicle_access.vehicle_id inner join tempaddplace ontempaddplace.Jrid=JOURNEY_REGT_DET.JOURNEY_ID where assign_vehicle_access.Assign ='''+@yes+''' and assign_vehicle_access.user_id = ''' + @user_id + ''' and tempaddplace.Timerecorded between Convert(datetime,'''+@frtime+''',103) and Convert(datetime,'''+@totime+''',103)'exec(@tablename)endGO-------------------------------i execute this by thisexec record 'USR012','11-11-2006 00:00:00:00','20-11-2006 00:00:00:00'But when i execute below line code this give error Invalid object name 'USR012'.select * from USR012Ranjeet Kumar Singh |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-03-09 : 02:43:18
|
Classical mistake!You are building your query in @Query variable but executing @tablename variable.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|
|
|
|