Execute sql text in SQL Server 2005

The following code give an error
 "Msg 2812, Level 16, State 62, Line 5
Could not find stored procedure 'select * from  Lumbartest'."
Is it possible to  execute code that has been placed in a varchar variable?


declare @cmd varchar(100)

set @cmd = 'select * from  Lumbartest'
exec @cmd
LVL 2
Roger AlcindorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roger AlcindorAuthor Commented:
Answered the problem myself :
declare @statement nvarchar(100)

set @statement = 'select * from  Lumbartest'
exec sp_executeSQL @statement
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roger AlcindorAuthor Commented:
I solved the issue myself
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
or:

exec(@cmd)
0
Roger AlcindorAuthor Commented:
Hello angellll,
Unfortunatelt your solution doesn't work.
The code below illustrates this, giving an error message
    Msg 137, Level 15, State 2, Line 1
    Must declare the scalar variable "@testdata".


      declare @testdata float
      declare @testnum int
      declare @secondlabel char(20)
      declare @badgenum int
      declare @statement nvarchar(200)
      declare @dtnow datetime

      set @dtnow = GetDate()
      set @badgenum = 24
      set @secondlabel = 'secondlabel'
      set @testnum = 1
      set @testdata = 2468
      set @statement = 'update LumbarTest set test1result = @testdata,DateTimeTest1 = GetDate(),operator = @badgenum  where Label2 like @secondlabel'

      print @statement
      if @testnum = 1
            exec (@statement)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.