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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Formatting in stored procedure

Author  Topic 

vishu_av
Yak Posting Veteran

69 Posts

Posted - 2007-06-01 : 08:40:03
Hi All..
I am trying to parameterize a tablename in a StoredProcedure something like below and use the below code in my other Stored procedure..

CREATE PROCEDURE SP_BACKUPTABLE @TABLENAME VARCHAR(255)
AS
DECLARE @BACKUPTABLE VARCHAR(255)
DECLARE @ALIASTABLE VARCHAR(255)
SET @BACKUPTABLE = @TABLENAME + '_BK'
SET @ALIASTABLE = @TABLENAME + '_ALIAS'
SELECT @ALIASTABLE.* FROM @TABLENAME AS @ALIASTABLE

i get error 'near .'...
How do i achieve the above code-functionality

Thanks in advance

mwjdavidson
Aged Yak Warrior

735 Posts

Posted - 2007-06-01 : 08:45:25
You'll have to use dynamic sql to achieve this - i.e. construct a SQL string and then execute it. Lookup "Dynamic SQL" in BOL for the specifics.

Mark
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-01 : 08:57:49
If you need to really send table name as parameter, use dynamic sql as suggested
Refer www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -