Control Freak Control Freak - 3 months ago 16
SQL Question

Must declare the scalar variable, Why when its a Param?

This gives me an error on

@ID
and
@Name
saying
Must declare the scalar variable
, but i don't understand why if it is a param. What am I doing wrong here?

CREATE PROCEDURE [dbo].[Ins_eProductFull]
@Name nvarchar(400)
,@ID int

AS
BEGIN

DECLARE @ID2 int

SET NOCOUNT ON;

INSERT INTO Table (Name) VALUES (@Name)

SET @ID2 = SCOPE_IDENTITY()

EXEC dbo.StoredProc1
@ID = @ID,
@Name = @Name,
@ID2 = @ID2

END
GO

Answer

only problem I see on your code is the first comma that should not be there. Other than that everything works fine

EDIT:

to whoever downvoted it, see for yourself:

create table Table1(Name varchar(50))

Create PROCEDURE [dbo].[Ins_eProductFull]
@Name nvarchar(400),
@ID int
AS
BEGIN
    DECLARE @ID2 int

    SET NOCOUNT ON;

    INSERT INTO Table1 (Name) VALUES (@Name)

    SET @ID2 = SCOPE_IDENTITY()

    EXEC dbo.StoredProc1 
      @ID = @ID, 
      @Name = @Name, 
      @ID2 = @ID2

END
GO

CREATE  procedure dbo.StoredProc1 
@ID int,
@Name varchar(50),
@ID2 int
as 
begin
    select @ID,@Name,@id2
end


exec [Ins_eProductFull] 'a',1

result:

1   a   NULL

and the NULL is because there is no identity on my test table

Comments