Error while assigning values to multiple variables in a single select (sql)

时间:2017-12-18 04:58:36

标签: sql-server

I have to create a procedure that takes 1 input, @assetid of type integer. The procedure must do the following operation:

Rules:

  1. If asset_id (i.e, assetid) passed as input matches with asset_id in asset_maintenance table, then calculate maintenance cost of that asset and insert a success_msg of type varchar(200) i.e, 'Maintenance cost of the asset name 'asset_name' is 'maint_cost'' into success_log table.

  2. If asset_id (i.e, assetid) passed as input does not match with asset_id in asset_maintenance table, then catch the exception and insert an error_msg of type varchar(200) i.e, 'No such asset' into errors_log table.

My query is as below:

CREATE PROCEDURE get_cost
    @assetid AS INT
AS
BEGIN
    DECLARE @msg VARCHAR(200), @assetname AS VARCHAR(60), 
            @assetmain AS FLOAT
BEGIN TRY
    IF EXISTS (SELECT @assetname = asset.name, @assetmain = asset_maintenance.cost 
               FROM asset 
               JOIN asset_maintenance ON asset.id = asset_maintenance.asset_id
               WHERE asset_maintenance.id  = @assetid)
        SET @msg = 'Maintenance cost of the asset ' + @assetname + 'is' + @assetmain

    INSERT INTO success_log (success_msg) VALUES (@msg)
END TRY
BEGIN CATCH
    SET @msg = 'No such asset'

    INSERT INTO errors_log (error_msg) VALUES (@msg)
END CATCH
END

I am getting the error as below:

Incorrect syntax near '='( does not give me the line number where the error occurs but the error occurred after I changed the Select statement in the IF EXISTS clause)

Any help would be greatly appreciated.

2 个答案:

答案 0 :(得分:0)

Looking at your code, I feel like you can handle this little easier without needing and additional variables. Like this

Create procedure get_cost
@assetid as int
AS
BEGIN
    Begin try
        IF exists (Select 1 from asset join asset_maintenance on asset.id = asset_maintenance.asset_id where asset_maintenance.id  = @assetid)
        BEGIN

            INSERT INTO success_log(success_msg)
            SELECT
                'Maintenance cost of the asset ' +asset.name+' is '+asset_maintenance.cost 
               FROM asset 
               JOIN asset_maintenance ON asset.id = asset_maintenance.asset_id
               WHERE asset_maintenance.id  = @assetid

        END
        ELSE
        BEGIN
            insert into errors_log (error_msg) VALUES ('No such asset')
        END 

    END TRY
    BEGIN CATCH
        insert into errors_log (error_msg) SELECT ERROR_MESSAGE()

    end catch
END

答案 1 :(得分:0)

First of all - you cannot assign values in an IF EXISTS() statement - and secondly, if that value does not exist, you're not going to get an "exception" that is caught by the BEGIN CATCH ... END CATCH block - you'll just get back a "false" value for IF EXISTS().

Change your code to:

CREATE PROCEDURE get_cost
    @assetid AS INT
AS
BEGIN
    DECLARE @msg VARCHAR(200), @assetname AS VARCHAR(60), 
            @assetmain AS FLOAT

    -- check if exists
    IF EXISTS (SELECT *
               FROM asset 
               JOIN asset_maintenance ON asset.id = asset_maintenance.asset_id
               WHERE asset_maintenance.id  = @assetid)
    BEGIN
        -- if yes - assign values, create success message
        SELECT @assetname = asset.name, @assetmain = asset_maintenance.cost 
        FROM asset 
        JOIN asset_maintenance ON asset.id = asset_maintenance.asset_id
        WHERE asset_maintenance.id  = @assetid)

        -- concatenating a string (asset name) and a numerical value (asset main) 
        -- requires that you CAST the numerical value to a varchar
        SET @msg = 'Maintenance cost of the asset ' + @assetname + 'is' + CAST(@assetmain AS VARCHAR(15))

        INSERT INTO success_log (success_msg) VALUES (@msg)
    END
    ELSE
    BEGIN
        -- if no - create error message
        SET @msg = 'No such asset'

        INSERT INTO errors_log (error_msg) VALUES (@msg)
   END 
END