【问题标题】:Msg 102, Level 15, State 1, Line 15消息 102,第 15 级,状态 1,第 15 行
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这是你的问题

    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;
    

    【讨论】:

    • 谢谢,我执行时不再有任何错误但我的第二个表中没有插入数据
    • @almaje 代码中的字符串值,应该是查询吗?
    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多