CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2007
    Posts
    122

    problem with output stored procedre

    i have problem with stored procedure , i create procedure and it run good .
    Code:
    use MyTestDataBase
    GO
    create proc sp_with_outpt_parameters2
    @id int ,
    @name nvarchar(40)=Null,
    @age int,
    @phone nvarchar (40)=Null,
    @CustomerId int output
    AS
    INSERT INTO Shippers VALUES (@id,@name,@age,@phone) 
    SELECT @CustomerId=@@Identity
    but when i use this stored procedure it give me this message
    -------------------------------------------------------------------
    Msg 137, Level 15, State 2, Line 11
    Must declare the scalar variable "@CustomerId".
    -----------------------------------------------------------

    Code:
    DECLARE @MyIdent int
    EXEC sp_with_outpt_parameters
    @id=10,
    @name = 'Ahmed',
    @age = 35,
    @phone = '15121052',
    @CustomerId = @MyIdent OUTPUT
    SELECT @MyIdent AS IdentityValue
    SELECT id,[name], age,phone
    FROM Shippers 
    WHERE @CustomerId = @MyIdent

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: problem with output stored procedre

    I think this is what you really want:
    SELECT id,[name], age,phone
    FROM Shippers
    WHERE CustomerId = @MyIdent
    Note that the where statement compares CustomerId columns to @MyIdent variable.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: problem with output stored procedre

    you should put your compound statements inside a BEGIN-END block

    Code:
    use MyTestDataBase
    GO
    create proc sp_with_outpt_parameters2
    @id int ,
    @name nvarchar(40)=Null,
    @age int,
    @phone nvarchar (40)=Null,
    @CustomerId int output
    AS
    BEGIN
       INSERT INTO Shippers VALUES (@id,@name,@age,@phone) 
       SELECT @CustomerId=@@Identity
    END
    Busy

  4. #4
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: problem with output stored procedre

    Quote Originally Posted by Thread1
    you should put your compound statements inside a BEGIN-END block
    No, Insert...Select is a single statement.
    Also, the error that the OP gets is in the execution script not the SP creation script.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  5. #5
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: problem with output stored procedre

    How about using a SET statement instead?

    Not sure if you can use:
    SELECT @CustomerId=@@Identity

    But you can definitly use Set @CustomerId = Ident_Current('Shippers') - well you can in SQL Server.

    HTH

    JP
    JP

    Please remember to rate all postings.

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: problem with output stored procedre

    Quote Originally Posted by hspc
    No, Insert...Select is a single statement.
    Also, the error that the OP gets is in the execution script not the SP creation script.
    I still find it as compound statement. Anyway, yes I overlooked that script I thought it was the creation of the SP hehe
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured