Simple Stuff...but I don't know it.
-
Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.
Where exactly are you defining a temporary table in this example?
-
Where exactly are you defining a temporary table in this example?
-
Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.
-
Hi, I'm not. Just want to use a variable to access an exisiting table. Select * from @parameter is this not possible? Thanks for the interest
-
have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try...
@SqlQuery = 'Select * from ' + @MyParam EXEC sp_executesql @SqlQuery
Regards
- J O H N -
You should always enclosed the table name in side square brackets just in case it contains any odd characters and the table name should be checked to see if the table exists first. These are very basic security precautions against invalid or maliceous data from causing problems with the database.
Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website
-
have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try...
@SqlQuery = 'Select * from ' + @MyParam EXEC sp_executesql @SqlQuery
Regards
- J O H N -
-
Thanks John, but declare @SqlQuery varchar set @SqlQuery = 'Select * from raiser wHERE raiser.raiserid=3' EXEC @SqlQuery Server: Msg 2812, Level 16, State 62, Line 5 Could not find stored procedure 'S'. doesn't work what am I doing wrong?
-
Why does this not work.....SQL Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' Select * from @MyParam Seems obvious that it should, is it all TEMP TABLES needed.
hi there, in this example you didn't create any table. You creating a variable and it is not possible to select from variable, -- Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' you can simple select like this: SELECT @MyParam -- it will give this result 'MyTableName' if you want to create table, use this Create Table #MyTableName ( here you define the attributes ex. atributename varchar(20) ) and then you can use SELECT * FROM #MyTableName the result --- atributename respect
spaps
-
hi there, in this example you didn't create any table. You creating a variable and it is not possible to select from variable, -- Declare @MyParam varchar (20) Set @MyParam = 'MyTableName' you can simple select like this: SELECT @MyParam -- it will give this result 'MyTableName' if you want to create table, use this Create Table #MyTableName ( here you define the attributes ex. atributename varchar(20) ) and then you can use SELECT * FROM #MyTableName the result --- atributename respect
spaps
-
Cheers guys, job done via Dynamic SQL. Why I can't lookup a variable table name, who knows? Seems stupid to not be able to. Thanks again.