【发布时间】:2017-05-07 03:30:16
【问题描述】:
我写了我的程序,我有这个错误:
Declare @code1 varchar(10), @code2 varchar(10), @lettre2 varchar(5), @lettre3 varchar(5), @lettre4 varchar(5);
SET @code2 = NULL;
SET @lettre2 = NULL;
SET @lettre3 = NULL;
SET @lettre4 = NULL;
DECLARE curseur1 CURSOR FOR
SELECT TOP 1 Code
FROM Configuration..ngap
ORDER BY 'Code';
OPEN curseur1
FETCH NEXT FROM curseur1 INTO @code1;
WHILE @@FETCH_STATUS = 0
BEGIN
if @code1 <> @code2
INSERT INTO Configuration..ngap2
VALUES ('@code1', 'select top 1 Description_acte from Configuration..ngap where Code='@code1' order by 'Code'', 'select top 1 Lettre from Configuration..ngap where Code='@code1' order by 'Code'', 'select top 1 Coef from Configuration..ngap where Code='@code1' order by 'Code'')
if @code1 = @code2
SET @lettre2 = (select lettre2 from Configuration..ngap2 where Code='@code1');
SET @lettre3 = (select lettre3 from Configuration..ngap2 where Code='@code1');
SET @lettre4 = (select lettre4 from Configuration..ngap2 where Code='@code1');
if @lettre2 IS NULL
UPDATE Configuration..ngap2
SET lettre2 = (select Lettre from Configuration..ngap where Code='@code1' and index_count=2)
UPDATE Configuration..ngap2
SET Coef2 = (select Coef from Configuration..ngap where Code='@code1' and index_count=2)
if @lettre3 IS NULL
UPDATE Configuration..ngap2
SET lettre3 = (select Lettre from Configuration..ngap where Code='@code1' and index_count=3)
UPDATE Configuration..ngap2
SET Coef3 = (select Coef from Configuration..ngap where Code='@code1' and index_count=3)
if @lettre4 IS NULL
UPDATE Configuration..ngap2
SET lettre4 = (select Lettre from Configuration..ngap where Code='@code1' and index_count=4)
UPDATE Configuration..ngap2
SET Coef4 = (select Coef from Configuration..ngap where Code='@code1'and index_count=4)
@code2=@code1;
FETCH NEXT FROM curseur1 into @code1;
END
CLOSE curseur1;
DEALLOCATE curseur1;
GO
错误:
消息 102,第 15 级,状态 1,第 15 行
'@code1' 附近的语法不正确。消息 102,第 15 级,状态 1,第 29 行
'@code2' 附近的语法不正确。
---------- 编辑 现在我有这个问题:
Declare @code1 varchar(10),@code2 varchar(10), @lettre2 varchar(5), @lettre3 varchar(5), @lettre4 varchar(5);
SET @code2 = 'aaa';
SET @lettre2 = NULL;
SET @lettre3 = NULL;
SET @lettre4 = NULL;
DECLARE curseur1 CURSOR FOR
SELECT top 1 Code from Configuration..ngap order by 'Code';
OPEN curseur1
Fetch next from curseur1 INTO @code1;
while @@FETCH_STATUS = 0
BEGIN
if @code1<>@code2
begin
Print @code1;
print @code2;
insert into Configuration..ngap2 (Code, Description_acte, Lettre, Coef)
values ('@code1',
'select top 1 Description_acte from Configuration..ngap where Code=' + @code1 +' order by Code',
'select top 1 Lettre from Configuration..ngap where Code=' + @code1 +' order by Code',
'select top 1 Coef from Configuration..ngap where Code=' + @code1+ ' order by Code')
end
if @code1=@code2
begin
set @lettre2 = (select lettre2 from Configuration..ngap2 where Code=@code1);
set @lettre3 = (select lettre3 from Configuration..ngap2 where Code=@code1);
set @lettre4 = (select lettre4 from Configuration..ngap2 where Code=@code1);
if @lettre2 is null
begin
update Configuration..ngap2 set lettre2 = (select Lettre from Configuration..ngap where Code=@code1 and index_count=2)
update Configuration..ngap2 set Coef2 = (select Coef from Configuration..ngap where Code=@code1 and index_count=2)
end
if @lettre3 is null
begin
update Configuration..ngap2 set lettre3 = (select Lettre from Configuration..ngap where Code=@code1 and index_count=3)
update Configuration..ngap2 set Coef3 = (select Coef from Configuration..ngap where Code=@code1 and index_count=3)
end
if @lettre4 is null
begin
update Configuration..ngap2 set lettre4 = (select Lettre from Configuration..ngap where Code=@code1 and index_count=4)
update Configuration..ngap2 set Coef4 = (select Coef from Configuration..ngap where Code=@code1 and index_count=4)
end
end
set @code2=@code1;
FETCH NEXT FROM curseur1 into @code1;
END
CLOSE curseur1;
DEALLOCATE curseur1;
GO
A115 aaa Msg 8152, Level 16, State 14, Line 19 字符串或二进制数据 会被截断。声明已终止。
【问题讨论】:
-
根据您的缩进仅供参考,只有
IF之后的第一行绑定到语句,要有条件地运行多个语句,您需要将它们包装在begin/end中 -
我这样做了,但我总是遇到同样的问题
标签: sql-server database tsql cursor procedure