Msg 102,Level 15,State 1,Line 15

时间:2016-12-21 16:17:59

标签: sql-server database tsql cursor procedure

我写了我的程序,我有这个错误:

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

错误:

  

Msg 102,Level 15,State 1,Line 15
  '@ code1'附近的语法不正确。

     

Msg 102,Level 15,State 1,Line 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字符串或二进制数据   会被截断。声明已经终止。

2 个答案:

答案 0 :(得分:1)

这是你的问题

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'')

您正在尝试将字符串运行为值...

尝试:

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')

第二个问题:

@code2=@code1;

尝试

set @code2=@code1;

答案 1 :(得分:0)

  

A115 aaa Msg 8152,Level 16,State 14,Line 19字符串或二进制数据   会被截断。声明已经终止。

某些字段(Code,Description_acte,Lettre,Coef)的大小小于字符串的长度。增加这些字段的大小,或截断长字符串(可能使用LEFT)