Author |
Topic |
Kalaiselvan
Posting Yak Master
112 Posts |
Posted - 2010-11-09 : 02:49:28
|
DECLARE @TableName VARCHAR(50)SET @TableName=(SELECT TableName FROM CRMCustomer WHERE CRMId=21)Here am getting a Table name in a variable.. Table name is coming..SELECT * FROM @TableNameBut when I using the variable name to select the table its not working fine. Asking to Declare the Table variable.Please help me in this to Select the Table Details from the Selected Variable name.Regards,Kalaiselvan RLove Yourself First.... |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
|
sathiesh2005
Yak Posting Veteran
85 Posts |
Posted - 2010-11-09 : 04:40:22
|
Try this..Declare @SQL varchar(1000)DECLARE @TableName VARCHAR(50)SET @TableName=(SELECT TableName FROM CRMCustomer WHERE CRMId=21)set @SQL ='select * from '+@TableNameExecute(@SQL)@Sathiesh |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-09 : 05:34:51
|
This is subject to sql injection and you need to take care of itMadhivananFailing to plan is Planning to fail |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 02:23:47
|
quote: Originally posted by madhivanan This is subject to sql injection and you need to take care of itMadhivananFailing to plan is Planning to fail
I guess the way to do it is thisdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='tablename'SET @SQL='select * from ' + @TableNameexec sp_executesql @SQL, N'@TableName varchar(50)',@TableName PBUH |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-10 : 08:21:46
|
quote: Originally posted by Sachin.Nand
quote: Originally posted by madhivanan This is subject to sql injection and you need to take care of itMadhivananFailing to plan is Planning to fail
I guess the way to do it is thisdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='tablename'SET @SQL='select * from ' + @TableNameexec sp_executesql @SQL, N'@TableName varchar(50)',@TableName PBUH
create table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects;delete from #t'SET @SQL='select * from ' + @TableNameexec sp_executesql @SQL, N'@TableName varchar(50)',@TableNameselect * from #tMadhivananFailing to plan is Planning to fail |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-10 : 08:26:48
|
create table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects;delete from #t'SET @SQL='select * from ' + quotename(@TableName)exec sp_executesql @SQL, N'@TableName varchar(50)',@TableName...gives error and nothing is deleted  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 08:31:40
|
This one does notcreate table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects'SET @SQL='select * from ' + quotename(@TableName) +';delete #t'print(@sql)exec sp_executesql @SQL, N'@TableName varchar(50)',@TableNamedrop table #t PBUH |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-11-10 : 08:34:52
|
quote: Originally posted by Sachin.Nand This one does notcreate table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects'SET @SQL='select * from ' + quotename(@TableName) +';delete #t'print(@sql)exec sp_executesql @SQL, N'@TableName varchar(50)',@TableNamedrop table #t PBUH
The point is, that <';delete #t'> isn't coming from outside. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 08:39:48
|
quote: The point is, that <';delete #t'> isn't coming from outside.
Oh sorry.PBUH |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-10 : 08:57:25
|
quote: The point is, that <';delete #t'> isn't coming from outside.
But then there is no need for using sp_executesql.Even this can suffice if I am not missing something.create table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects;delete #t'SET @SQL='select * from ' + quotename(@TableName)exec(@sql)drop table #t PBUH |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
glendcruz
Yak Posting Veteran
60 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-11 : 04:34:29
|
Post the code you usedMadhivananFailing to plan is Planning to fail |
 |
|
glendcruz
Yak Posting Veteran
60 Posts |
Posted - 2010-11-11 : 17:07:14
|
quote: Originally posted by madhivanan Post the code you usedMadhivananFailing to plan is Planning to fail
It is the same code you designed.create table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects;delete from #t'SET @SQL='select * from ' + @TableNameexec sp_executesql @SQL, N'@TableName varchar(50)',@TableNameselect * from #tThanks once againGlen |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-11-12 : 05:13:41
|
What is the error?MadhivananFailing to plan is Planning to fail |
 |
|
glendcruz
Yak Posting Veteran
60 Posts |
Posted - 2010-11-12 : 21:55:45
|
I tried the same code but I am getting the following errors belowOlease show me where I am going wrong.create table #t (i int)insert into #t select 1select i from #tGOdeclare @sql nvarchar(max)declare @TableName nvarchar(max)=''set @TableName='sysobjects;delete from #t'SET @SQL='select * from ' + @TableNameexec sp_executesql @SQL, N'@TableName varchar(50)',@TableNameselect * from #tThanks once againGlen(1 row(s) affected)i-----------1(1 row(s) affected)Msg 139, Level 15, State 1, Line 0Cannot assign a default value to a local variable.Msg 137, Level 15, State 1, Line 3Must declare the scalar variable "@TableName".Msg 137, Level 15, State 2, Line 4Must declare the scalar variable "@TableName".Msg 137, Level 15, State 2, Line 5Must declare the scalar variable "@TableName". |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2010-11-12 : 23:12:38
|
declare @TableName nvarchar(max) -----=''the <=''> is a default value. You can only do that for stored procedure parameters not local variables.Be One with the OptimizerTG |
 |
|
Sachin.Nand
2937 Posts |
Posted - 2010-11-12 : 23:23:55
|
quote: I tried the same code but I am getting the following errors below
Are you using SQL 2008 because with sql2005 u cannot do this.declare @TableName nvarchar(max)=''Instead do thisdeclare @TableName nvarchar(max)set @TableName=''PBUH |
 |
|
glendcruz
Yak Posting Veteran
60 Posts |
Posted - 2010-11-12 : 23:36:24
|
Thanks TG and SashinI got a lot of information , but I dont think that is what the requirement is in the orginal query. Still unconvinced, but would like to hear your openion if the answer is appropriate to the Question put at first------------------- Thanks TG and Sashin . I cannot understand the where clause WHERE CRMId=21)and what is CRMID = 21. The code you gave runs well.DECLARE @TableName VARCHAR(50)SET @TableName=(SELECT TableName FROM CRMCustomer WHERE CRMId=21)Here am getting a Table name in a variable.. Table name is coming..SELECT * FROM @TableNameBut when I using the variable name to select the table its not working fine. Asking to Declare the Table variable.Please help me in this to Select the Table Details from the Selected Variable name. |
 |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2010-11-13 : 09:07:35
|
quote: Originally posted by glendcruz would like to hear your openion if the answer is appropriate to the Question put at first
It's not clear if you mean YOUR first question or the original poster's first question. But in either case neither of you actually ASKED a question. The code you posted works for me once I removed the default value set in the local variable: @tableNameBe One with the OptimizerTG |
 |
|
Next Page
|