Setting parameter value using SQL Query

I have a variable in an sql query "@Datarows" that I need to assign a value to. My issue is that I am creating a one SQL for all type of solution, therefore, I have variable "Staging_Table" that changes from time to time. So, I assign a value to the Staging Table and trying to set the @Datarows value, however, I'm getting an error "@DataRows" must be declared. I have already declared the variable, so I have something wrong with my syntax. I have included the SQL below. Thanks.

Declare @SQL VarChar(Max)
Declare @Staging_Table VarChar(255)
Declare @DataRows BigInt

Set @SQL = 'Set @DataRows = (Select Count(File_ID) From ' + @Staging_Table + ')'
exec (@SQL)
OB1CanobieAsked:
Who is Participating?
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.

Asim NazirCommented:
Do it like this:

Declare @SQL VarChar(Max)
Declare @Staging_Table VarChar(255)

set @Staging_Table = 'SB_CODEINFO_SIC'
Set @SQL = ' Declare @DataRows BigInt; (Select @DataRows = Count(File_ID) From ' + @Staging_Table + '); print @DataRows  '
exec (@SQL)
0
Asim NazirCommented:
I Removed following line:
set @Staging_Table = 'SB_CODEINFO_SIC'
it should be set in your parameter. I did for testing.

Declare @SQL VarChar(Max)
Declare @Staging_Table VarChar(255)
Set @SQL = ' Declare @DataRows BigInt; (Select @DataRows = Count(File_ID) From ' + @Staging_Table + '); print @DataRows  '
exec (@SQL)
0
VipulKadiaCommented:
It will help you :

      declare @str nvarchar(1000)
      declare @val numeric
      declare @valOUT numeric
      DECLARE @ParmDefinition NVARCHAR(500)

      set @val = 0
      set @str = ''
      set @str = 'select @val = count(*) from sysobjects'
      SET @ParmDefinition = N'@val numeric OUTPUT'

      EXECUTE sp_executesql @str, @ParmDefinition,  @val OUTPUT

      print @str
      --exec(@str)
      print 'VALUE'
      print @val
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
Alpesh PatelAssistant ConsultantCommented:
Declare it in you dynamic SQL statement
0
OB1CanobieAuthor Commented:
Your solution worked great.
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
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.