samliam
asked on
table variable
I am trying to pass a table_name into a stored proc. What's wrong with the following?
Create Proc InsertProc
@table ( {col1 varchar(20) col2 varchar(20)} ),
@col1,
@col2
AS
Insert @table (col1, col2)
values(@col1, @col2)
GO
Should it be done differently if the table exists or if the table does not exist?
Create Proc InsertProc
@table ( {col1 varchar(20) col2 varchar(20)} ),
@col1,
@col2
AS
Insert @table (col1, col2)
values(@col1, @col2)
GO
Should it be done differently if the table exists or if the table does not exist?
ASKER
Is your code supposed to work if I just copy and paste?
I changed '@tableName' to '@table' which I suppose is typo?
It says: Incorrect syntx near ','.
I changed '@tableName' to '@table' which I suppose is typo?
It says: Incorrect syntx near ','.
From BOL
<quote>
Syntax
CREATE PROC [ EDURE ] procedure_name [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
...
data_type
Is the parameter data type. All data types, except the table data type, can be used as a parameter for a stored procedure. However, the cursor data type can be used only on OUTPUT parameters. When you specify a data type of cursor, the VARYING and OUTPUT keywords must also be specified. For more information about SQL Server - supplied data types and their syntax, see Data Types.
</quote>
Anthony
<quote>
Syntax
CREATE PROC [ EDURE ] procedure_name [ ; number ]
[ { @parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
...
data_type
Is the parameter data type. All data types, except the table data type, can be used as a parameter for a stored procedure. However, the cursor data type can be used only on OUTPUT parameters. When you specify a data type of cursor, the VARYING and OUTPUT keywords must also be specified. For more information about SQL Server - supplied data types and their syntax, see Data Types.
</quote>
Anthony
In other words, you cannot pass a parameter of data type table. For other alternatives consider maintaining your many old open questions and you may find experts more responsive:
1 04/04/2001 50 monitoring system resources Open Windows Programming
2 02/03/2002 100 read pdf from IE6 Open Windows NT
3 10/13/2003 50 automatically generated statement Open C#
4 10/13/2003 50 the Duwamish online project Open C#
5 10/20/2003 100 webform creation at runtime? Open C#
6 09/25/2001 50 network cards Open Windows NT
7 10/07/2003 125 IE6 runtime error Open JavaScript
8 10/07/2003 20 IE6 debug? Open Browsers Issues
Thanks,
Anthony
1 04/04/2001 50 monitoring system resources Open Windows Programming
2 02/03/2002 100 read pdf from IE6 Open Windows NT
3 10/13/2003 50 automatically generated statement Open C#
4 10/13/2003 50 the Duwamish online project Open C#
5 10/20/2003 100 webform creation at runtime? Open C#
6 09/25/2001 50 network cards Open Windows NT
7 10/07/2003 125 IE6 runtime error Open JavaScript
8 10/07/2003 20 IE6 debug? Open Browsers Issues
Thanks,
Anthony
samliam,
If the table already exists:
1. Do the concantination in your VB code if possible. If not possible you will need to use Dynamic SQL to achieve this result:
Create Proc InsertProc
@table varchar(50)
@col1 varchar(50),
@col2 varchar(50)
AS
Exec("Insert " + @table + " values('" + @col1 + "', '" + @col2 + "'")
GO
2. If the table does not exist you would have to create it in the store procedure or by using SQL DMO from your code
Leon
If the table already exists:
1. Do the concantination in your VB code if possible. If not possible you will need to use Dynamic SQL to achieve this result:
Create Proc InsertProc
@table varchar(50)
@col1 varchar(50),
@col2 varchar(50)
AS
Exec("Insert " + @table + " values('" + @col1 + "', '" + @col2 + "'")
GO
2. If the table does not exist you would have to create it in the store procedure or by using SQL DMO from your code
Leon
ASKER
Is your code the same as this?
Create Proc InsertProc
@table varchar(50),
@col1 varchar(50),
@col2 varchar(50)
AS
insert @table values (@col1, @col2)
GO
Error: "Must declare the vaiable @table"
Create Proc InsertProc
@table varchar(50),
@col1 varchar(50),
@col2 varchar(50)
AS
insert @table values (@col1, @col2)
GO
Error: "Must declare the vaiable @table"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have hard time with the double and single quotes. Your code has syntax error. Have you set the quoted_identifier ON? I can save the stored proc but I am not able to execute my C# program that calls the stored proc.
btw, how do you debug a stored proc?
btw, how do you debug a stored proc?
>>how do you debug a stored proc? <<
This is built into SQL Query Analyzer in SQL 2K
Do you need any help maintaining your open questions?
Anthony
This is built into SQL Query Analyzer in SQL 2K
Do you need any help maintaining your open questions?
Anthony
ASKER
I already posted a request in the comunity suport area:
https://www.experts-exchange.com/questions/20794558/working-Please-delete-questions.html
https://www.experts-exchange.com/questions/20794558/working-Please-delete-questions.html
ASKER
According to the SQL server book, Exec takes single-quoted string as argument?
EXEC [ UTE ] ( { @string_variable | [ N ] 'tsql_string' } [ + ...n ] )
EXEC [ UTE ] ( { @string_variable | [ N ] 'tsql_string' } [ + ...n ] )
Ok then change it to:
Exec('Insert ' + @table + ' values(' + @col1 + ', ' + @col2)
Make sure the store procedure runs before attempting to run it from your code.
Leon
Exec('Insert ' + @table + ' values(' + @col1 + ', ' + @col2)
Make sure the store procedure runs before attempting to run it from your code.
Leon
try this.
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER PROCEDURE setDestination
@tableName varchar(50) = null,
@col1,
@col2
AS
declare @sql nvarchar(4000)
if (@tableName is null or @tableName = '')
begin
/**********put some error handler here if tablename is null******/
return 1
end
set @sql = 'Insert @table (col1, col2) values(@col1, @col2)'
execute sp_executesql @sql
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO