我正在尝试在表上执行Update语句。此语句位于Cursor和While块中。我已经检查了调试器,并且值正在进入语句和变量,但更新仍未将值放入表字段中。请告诉我这里做错了什么。
ALTER PROCEDURE SP_PO1 @P1 int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @DOC AS INT;
DECLARE @CASH AS FLOAT;
DECLARE @TENTYPE AS VARCHAR(100);
DECLARE @UDF AS VARCHAR(100);
DECLARE @COUNTER AS INT;
DECLARE @SQL AS VARCHAR(500);
SELECT @DOC=DOCTYPE FROM InvNum WHERE AutoIndex = @P1;
IF @DOC = 6
BEGIN
SET @COUNTER = 1;
DECLARE Cur_Tender CURSOR FOR
SELECT Tender.TenderNo FROM Tender;
OPEN CUR_TENDER;
FETCH NEXT FROM CUR_TENDER INTO @TENTYPE;
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @CASH = ISNULL(_btblPOSTenderTx.fTxAmount,0) FROM _btblPOSTenderTx INNER JOIN Tender ON _btblPOSTenderTx.iTenderID = Tender.IdTender INNER JOIN _btblPOSXZTable ON _btblPOSTenderTx.iPOSXZTableID = _btblPOSXZTable.IDPOSXZTable WHERE (_btblPOSXZTable.iTillTxType = 7) and (_btblPOSXZTable.IDPOSXZTable = (select Max(IDPOSXZTable) from [dbo].[_btblPOSXZTable])) AND (TenderNo = @TENTYPE);
SET @UDF = 'ufIDPOSInvTENDER' + CONVERT(VARCHAR(2),@COUNTER);
UPDATE InvNum SET @UDF=@CASH WHERE AutoIndex = @P1;
SET @COUNTER = @COUNTER + 1;
FETCH NEXT FROM CUR_TENDER INTO @TENTYPE;
END
END
CLOSE CUR_TENDER
DEALLOCATE CUR_TENDER
END
GO
答案 0 :(得分:0)
您更新变量
UPDATE InvNum SET @UDF=@CASH WHERE AutoIndex = @P1;
更新表格列
UPDATE InvNum SET <column>=@CASH WHERE AutoIndex = @P1;
如果你想要一个动态列名 - 使用dynamic sql
EXEC('UPDATE InvNum SET ' + @UDF + '=' + CAST(@CASH as VARCHAR(50) + ' WHERE AutoIndex = ' + CAST(@P1 as VARCHAR(5) ' );