| Author |
Topic |
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 14:52:08
|
| hiI want to create some table baseed on input parameterCREATE PROCEDURE ABC @tabname varchar(40)AS DECLARE @tabNM vachar (40)Set @filmn=abc_dce_+@tabnameCREATE TABLE @filmn( field1 varchar (50) AS NULL , field2 varchar (10)GoFor Executionexec abc xyzIam using the above code where xyz is input parameter, what i want is the tble name should be dynmic and whatever parameter i supply it will create a new tableThanks in advnce |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-01 : 14:53:28
|
| This is called dynamic sql. There are hundreds if not thousands of examples in the forums. Just do a forum search and you'll see.Why would you want to do this though? Just use a temp table instead.Tara |
 |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 14:59:57
|
| thanksWhy I need cz every time we have to enter in different tablesso if u know the exact solution plz reply me otherwise i will search for it |
 |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 16:36:20
|
| I have an error at @tabname so can i use variable with create tableCREATE TABLE 'abc_dec_'+@tabnameORCREATE TABLE @tabnameplz help |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 16:59:03
|
| Thanks for ur quick replyYes that exapmle is gr8 but still when iam runnig my command I have an error in create table @tablenamethe error is at @tablenmeplz help me |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-01 : 17:01:07
|
| Did you change your stored procedure to incorporate dynamic sql? That's the only way that it is going to work. What exactly are you running? Show me how you added the dynamic sql into it.Tara |
 |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 17:25:07
|
| yesI dont know how to use dynamic sql its first time iam using.CREATE PROCEDURE abc_sss @x varchar(40)AS Declare @y varchar(40)Set @y ='abc_dec_'+@xCREATE TABLE @y( Field1 varchar NULL , Field2 varchar NULL )So this is my codeand when i run i have error at CREATE TABLE @y at @y which is my variablethanks again |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-01 : 17:27:47
|
| I don't see where you used the example in the post to fix your stored procedure. Here is an example of dynamic sql for a SELECT statement:DECLARE @SQL VARCHAR(7000)DECLARE @TblName SYSNAMESET @TblName = 'Table1'SET @SQL = 'SELECT * FROM ' + @TblNameEXEC (@SQL)You need to understand what dynamic sql is doing. You are building a dynamic string to be executed. That is what you must do in your code. The building part is done at SET @SQL...Tara |
 |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-12-01 : 20:43:28
|
| CREATE PROCEDURE ccc @x varchar(40)AS Declare @y varchar(40)Declare @sq varchar(255)Set @y='abc_dec_'+@xset @sq= 'CREATE TABLE'+ @y+'( Field1 varchar NULL , field2 varchar NULL,)'execute ccc testfileNow this is working with no error but there it is not creating any table |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-12-02 : 12:10:54
|
| You don't have EXEC (@sq) in your stored procedure. The SET statement just sets up the variable. You need to execute the variable. This is done inside the stored procedure.Tara |
 |
|
|
|