Stored procedure problem

  • Can somebody tell me what's wrong with the following code, please?

    set nocount on

    declare @varname varchar(50)

    declare @newownername varchar(20)

    set @newownername = 'dbo'

    declare @import table

    (

    rownum int identity (1, 1) primary key not null,

    tablename nvarchar(255)

    )

    insert into @import (tablename) values ('chjohnson.christest')

    select @varname = tablename from @import

    sp_changeobjectowner @varname, @newownername

    set nocount off

    go

    I get the following error.

    "Server: Msg 170, Level 15, State 1, Line 12

    Line 12: Incorrect syntax near '@varname'."

    Any help is appreciated.  Thanks.

    Chris

  • Try this

    set nocount on

    declare @varname varchar(50)

    declare @newownername varchar(20)

    set @newownername = 'dbo'

    declare @import table

    (

    rownum int identity (1, 1) primary key not null,

    tablename nvarchar(255)

    )

    insert into @import (tablename) values ('chjohnson.christest')

    select @varname = tablename from @import

    EXEC sp_changeobjectowner @varname, @newownername

    set nocount off

    go

  • That did it!  Thanks a lot, Sriram!  I guess the "exec" is needed in a stored procedure, huh?!

  • EXEC is short for EXECUTE. That's what causes the stored procedure to run (or be EXECuted).

    -SQLBill

  • Yeah, I know what "exec" is, but when you run a stored procedure in Query Analyzer, it's optional.  I was just commenting on the fact that it is required in stored procedure code.  Thanks.

  • Christopher: close interpretation of EXEC, but the requirement to use EXEC/EXECUTE is when there is more than one statement (the procedure call) in a batch.  You have to use EXEC in QA when you're calling multiple procedures, performing additional actions, declaring variables, etc.

    -Eddie

     

    Eddie Wuerch
    MCM: SQL

  • Dig it!  Thanks all!

Viewing 7 posts - 1 through 6 (of 6 total)

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