Skip to content

How do I use database and Tables as parameters

johannes johannes

Asked Apr 14, 2006, 1:32 AM EDT1 answer
Share
LinkedInX

Hi guys!

I want to set database as parameter so that when you select a data you chooos with database to use, I keep getting the same error, my stored procedure is as follows(runned successfully):

>>>>.
Create     procedure proc_TestingDatabases
@databasename varchar (30)
,@tablename varchar (30)

as

execute
('use' + @databasename + 'select * from' + @tablename)
>>>>>>>>>>

but on the application I receive an error saying incorrect systxt near (*).

Then I tested the following one from the book:

>>>>>>>>>>
DECLARE @databasename varchar(30), @tablename varchar(30)
SET @databasename = 'Northwind'
SET @tablename = 'Products'

EXECUTE
('USE' + @databasename + 'SELECT * FROM' + @tablename)
>>>>>>>>>>>>

I receive the same error, but the they have a result as follows:
ProductName
Chai
Chang
Anniseed Syrup


Is the book lying or there is something that I do wrong.

Thanks

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    I manage to work make the tabke work but when the database is removed, I can select a table a required table, but still I want to select a database so that I can specify which data I want to work with...

    The main thing is that I want to copy a table from one database and move it to another, so I have to select a table and database and to which database I will move it to. If that work, then I will try to create a table at run time (the user must be able to do that) but specify the table name, then it will be added on the select statement with the database 
    +1