开发者

Mixed Table Type with other types as parameters to Stored Procedured c#

开发者 https://www.devze.com 2022-12-17 06:36 出处:网络
I am asking about how could i pass multi parameters to a stored procedure, one of these parameters is user defined table. When I tried to do it it shows this error:

I am asking about how could i pass multi parameters to a stored procedure, one of these parameters is user defined table. When I tried to do it it shows this error:

INSERT INTO BD (ID, VALUE, BID) values( (SELECT t1.ID, t1.Val开发者_开发知识库ue FROM @Table AS t1),someintvalue) here @Table is the user defined table parameter.

Msg 116, Level 16, State 1, Procedure UpdateBD, Line 12 Only one expression can be specified in the select list when the subquery is not introduced with EXISTS. Msg 109, Level 15, State 1, Procedure UpdateBD, Line 11 There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

Thank you


You need to pass in the table name as a varchar and your query needs to be a varchar as well. It would look like:

CREATE PROCEDURE MyStoredProc(
    @MyTableNameParameter varchar(50) = null
)
AS

DECLARE @Query nvarchar(MAX)

SET @Query = N'SELECT * FROM ' + @MyTableNameParameter + ' WHERE 1=1'

EXEC (@Query)

GO

I would not recommend using this technique though because it is vulnerable to SQL injection

0

精彩评论

暂无评论...
验证码 换一张
取 消