我在执行存储过程(SP_StoredProcedure
)时遇到此错误:
dim TableName string = "Table1"
dim acctcode as string "Table1.acctcode"
dim month as integer = 1
dim year as integer = 2011
这是我执行存储过程(SP_StoredProcedure
)FROM VB.NET 的代码:
execute("EXECUTE SP_StoredProcedure '"& TableName &"','"& acctcode &"',"& month &","& year &"")
以下是SP_StoredProcedure
上的代码:
CREATE PROCEDURE [dbo].[SP_StoredProcedure]
@TableName as varchar(100),
@AcctCode as varchar(100),
@Month as numeric(18,0),
@Year as numeric(18,0)
AS
BEGIN
DECLARE @Query as varchar(1000)
SET @Query =
'UPDATE Table2 SET Dat = (SELECT Dat FROM '+ @TableName +' WHERE table2.acctcode = '+ @acctcode +' AND month = '+ @month +' AND year = '+ @year +') FROM Table2'
EXEC @Query
END
答案 0 :(得分:1)
在varchar字符串中进行动态查询时,需要转换数值,这对日期也很重要!否则它不会正确附加到@Query变量。
如果@acctcode是一个varchar,那么你需要使用额外的单引号来转义它,以便它能像字符串一样正确地处理它。
ie ..
SET @Query = ' UPDATE Table2 ' +
' SET Dat = ( ' +
' SELECT Dat FROM ' + @TableName +
' WHERE table2.acctcode = '''+ @acctcode +''' +
' AND month = ' + convert(varchar(18), @month) +
' AND year = '+ convert(varchar(18), @year) +
' ) ' +
' FROM Table2 '