Must declare the scalar variable when assigning SELECT statement to nvarchar variable

  • Being an adhoc coder I am trying to write a AP and getting the following error

    Msg 137, Level 15, State 2, Line 2

    Must declare the scalar variable "@SageDataBase".

    with this:

    DECLARE @sql nvarchar(2000)

    DECLARE @SageDataBase nvarchar(60)

    SET @sql = N'

    @SageDataBase =

    SELECT ACSODBC from tblCompany

    WHERE CompanyID = 1016'

    EXEC(@SQL)

    The Select statement should only return one value as CompanyID is the table index, in the real thing I am passing 1016 as a paramater.

    I want to go on and use @SageDataBase in another SELECT statement .... within the next select statement I can not create a derived table as @SageDataBase is the database in am looking the data up in..

  • DECLARE @SageDataBase NVARCHAR(60);

    SET @SageDataBase =

    (

    SELECT ACSODBC

    FROM tblCompany

    WHERE CompanyID = 1016

    );

  • DECLARE @SageDataBase NVARCHAR(60);

    SELECT @SageDataBase = ACSODBC

    FROM tblCompany

    WHERE CompanyID = 1016

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply