因此,当执行下面的SQL时,我得到以下内容:字符串或二进制数据将被截断。
EXEC sp_GoodsWelcomeNotification 'd84be19b-be86-4a38-958b-e6d786cc27db'
存储过程如下:
ALTER PROC [dbo].[sp_GoodsWelcomeNotification](
@ItemGuid VARCHAR(50)
)
AS
DECLARE @ProductCode VARCHAR(50), @ParentGuid VARCHAR(100)
SET @ProductCode = 'SELECT Idx37 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=' + @ItemGuid
SET @ParentGuid = 'SELECT Idx134 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=' + @ItemGuid
BEGIN TRANSACTION
INSERT INTO NotificationTable
VALUES(650, @ProductCode, @ParentGuid, 'PENDING','','','Y','Y',@ItemGuid,0,'','')
COMMIT TRANSACTION
当我手动执行insert语句时,它执行正常:
INSERT INTO NotificationTable
VALUES(650, 'BV-TKM-0000068-02', '241c8abc-5f95-4c35-9405-d6adfc8e2f55', 'PENDING','','','Y','Y','d84be19b-be86-4a38-958b-e6d786cc27db',0,'','')
我哪里错了?
答案 0 :(得分:4)
此语法不正确:
SET @ProductCode = 'SELECT Idx37 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=' + @ItemGuid
SET @ParentGuid = 'SELECT Idx134 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=' + @ItemGuid
我认为你在寻找:
SELECT @ProductCode = Idx37 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=@ItemGuid
SELECT @ParentGuid = Idx134 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6=@ItemGuid
您将SELECT
语句分配给变量,而不是select语句的结果
或者更简单的查询是:
SELECT
@ProductCode = Idx37,
@ParentGuid = Idx134
FROM
iwfAccountOpening
WHERE
Idx1!=''
AND Idx6=@ItemGuid
答案 1 :(得分:0)
试试这个,
ALTER PROC [dbo].[sp_GoodsWelcomeNotification](
@ItemGuid VARCHAR(50)
)
AS
DECLARE @ProductCode VARCHAR(50), @ParentGuid VARCHAR(100)
SET @ProductCode = (SELECT Idx37 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6= @ItemGuid)
SET @ParentGuid = (SELECT Idx134 FROM iwfAccountOpening WHERE Idx1!='' AND Idx6= @ItemGuid)
BEGIN TRANSACTION
INSERT INTO NotificationTable
VALUES(650, @ProductCode, @ParentGuid, 'PENDING','','','Y','Y',@ItemGuid,0,'','')
COMMIT TRANSACTION