0

I have the following stored procedure

CREATE PROCEDURE SigleEnseigne
     @Sigle VARCHAR OUTPUT,
     @Enseigne VARCHAR OUTPUT,
     @SigleEnseigne1 VARCHAR(150) OUTPUT
AS
BEGIN
    SELECT ISNULL(John_Jack.Sigle,'') 
    INTO Sigle 
    FROM John_Jack

    SELECT ISNULL(John_Jack.Enseigne,'') 
    INTO Enseigne 
    FROM John_Jack

    SELECT 
        CASE 
           WHEN Code_Juridique LIKE 'M%' THEN '      / ' + @Enseigne INTO @SigleEnseigne1
           WHEN @Sigle = '' AND @Enseigne = '' THEN '' INTO @SigleEnseigne1
           ELSE @Sigle + ' / ' + @Enseigne INTO @SigleEnseigne1
        END
    FROM John_Jack
END
GO

I've got this error code when creating the sp

Msg 156, Niveau 15, État 1, Procédure SigleEnseigne, Ligne 29
Syntaxe incorrecte vers le mot clé 'INTO'.

SQL Server doesn't like this line

WHEN Code_Juridique LIKE 'M%' THEN '      / ' + @Enseigne INTO @SigleEnseigne1

Can anyone give me some insight of what I'm doing wrong, please? I'm fairly new to SQL Server, my script may not respect the T-SQL standard. If so, please enlighten me.

Thanks

3
  • @Sigle <> Sigle,also learn to declare the size,VARCHAR is equivalent to VARCAHR(1) Commented Dec 10, 2015 at 10:25
  • What are you trying to achieve? Commented Dec 10, 2015 at 10:25
  • Hi @GiorgiNakeuri, I'm trying to put the relevant column data into the variable Sigle,Enseigne and SigleEnseigne1 , to be able to retrieve them when I'll use the sp in my reports Commented Dec 10, 2015 at 10:29

3 Answers 3

1

You have several problems here:

1. Give your types a length
2. Incorrect syntax like `INTO @SigleEnseigne1`
3. You can do this in one go

Here is corrected query:

CREATE PROCEDURE SigleEnseigne
    @Sigle VARCHAR(150) OUTPUT ,
    @Enseigne VARCHAR(150) OUTPUT ,
    @SigleEnseigne1 VARCHAR(150) OUTPUT
AS
    BEGIN

        SELECT TOP 1
                @Sigle = ISNULL(Sigle, '') ,
                @Enseigne = ISNULL(Enseigne, '') ,
                @SigleEnseigne1 = CASE WHEN Code_Juridique LIKE 'M%'
                                         THEN '      / ' + ISNULL(Enseigne, '')
                                       WHEN ISNULL(Sigle, '') = ''
                                            AND ISNULL(Enseigne, '') = ''
                                         THEN ''
                                       ELSE Sigle + ' / ' Enseigne
                                  END
        FROM    John_Jack
    END
GO

But this query makes no sense if John_Jack table has more then one row. In such a case you probably need to add some WHERE clause to the query to get the row you need.

Sign up to request clarification or add additional context in comments.

3 Comments

This could probably the answer of the question. I have not checked the variable length :(
@GiorgiNakeuri , I'm creating the sp to use it on a SSRS report. The previous report was built on crystal report and I'm translating it. From what I've read so far, SSRS should be able to put one record at a time. So I'm trying and if it is not doing what I'm lookingfor , I'll find another way
Declare @Sigle VARCHAR(150) , @Enseigne VARCHAR(150) , @SigleEnseigne1 VARCHAR(150) EXEC SigleEnseigne @Sigle OUTPUT, @Enseigne OUTPUT, @SigleEnseigne1 OUTPUT
1

It should be more like

WHEN something THEN SET @yourVariableField = whatever

The INTO statement is more if you're inserting data into a table or table variable

Comments

1

It should be like this -

SELECT @Sigle = ISNULL(John_Jack.Sigle,'') 
FROM John_Jack;

SELECT @Enseigne = ISNULL(John_Jack.Enseigne,'') 
FROM John_Jack;

SELECT @SigleEnseigne1 = CASE 
                WHEN Code_Juridique LIKE 'M%' THEN '      / ' + @Enseigne 
                WHEN @Sigle = '' AND @Enseigne = '' THEN '' 
                ELSE @Sigle + ' / ' + @Enseigne
                END
 FROM John_Jack

As you define output variable @SigleEnseigne1 VARCHAR(150) OUTPUT.

3 Comments

You are just dialing with syntax problem. OP has not only the syntax problem.
@GiorgiNakeuri: Because OP is define output variable as varchar.
Hi @KrishnrajRana , First, Thank You. Please see my comments below my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.