Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dynamic Query Problem
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01518212
Message ID:
01518228
Vues:
45
>I'm trying to run this query:
>
>
>DECLARE @voter_id INT
>SET @Command = 'SELECT @voter_id = voterid FROM tblCamp_CT WHERE VoterId = ' + CAST(@VoterId AS VARCHAR(20)) + ' AND ' + @Query
>EXEC (@Command)
>
>
>and I'm getting the error
>
>
>Must declare the scalar variable "@voter_id".
>
>
>What am I not seeing here???

You declared the variable as @Voter_ID, but using it later in the query as @VoterID (no _)

In addition, I suggest to not embed the variable into the query as you do.

Try:
declare @VoterID int, @SQL nvarchar(max)
set @VoterID = 1

set @SQL = 'SELECT top (1) @Voter_ID = VoterID FROM tblCamp_CT where VoterID = @VoterID  AND ' + @Query

execute sp_ExecuteSQL @SQL, N'@VoterID int OUTPUT' @VoterID OUTPUT
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform