Msg 102, Level 15, State 1, Line 15

262 Views Asked by At

I write my procedure and I have this errors :

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

Errors :

Msg 102, Level 15, State 1, Line 15
Incorrect syntax near '@code1'.

Msg 102, Level 15, State 1, Line 29
Incorrect syntax near '@code2'.

---------------------- EDIT Now I have this problem :

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 String or binary data would be truncated. The statement has been terminated.

2

There are 2 best solutions below

2
JohnHC On BEST ANSWER

This is your problem

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

You are trying to run the string into a value...

Try:

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

And the second issue:

@code2=@code1;

Try

set @code2=@code1;
0
Denis Rubashkin On

A115 aaa Msg 8152, Level 16, State 14, Line 19 String or binary data would be truncated. The statement has been terminated.

The size of some fields (Code, Description_acte, Lettre, Coef) is less than the length of the string. Increase the size of these fields, or truncate the long strings (may be using LEFT)