October 19, 2005 at 5:23 am
Hi,
Please look at code Snippets given below. When I run it gives me 'Must declare the variable '@count' ERROR.
Create Procedure XYZ
(
@name varchar(50)
@Last varchar(50)
)
AS
DECLARE @count int
DECLARE @strQuery varchar(4096)
SET @strQuery = 'Select @count = count(*) from app_client where first_name = ''' + @name + ''' AND last_name = ''' + @Last + ''''
EXEC (@strQuery)
IF @count = 0
BEGIN
SELECT 'Not Found'
END
GO
Please Help...
October 19, 2005 at 6:33 am
if one were to go only by the code snippet provided then it can all be condensed to...
IF NOT EXISTS(SELECT * FROM app_client WHERE first_name = @name AND last_name = @last) BEGIN SELECT 'Not Found' END
..(can't see the need for dynamic sql)
**ASCII stupid question, get a stupid ANSI !!!**
October 19, 2005 at 7:25 am
Makes no difference in a exists clause.
October 19, 2005 at 7:29 am
Thank you remi - so David - there's your answer - mine was actually a trap to lure in those who hadn't known about the * & exists combo...GOTCHA!
**ASCII stupid question, get a stupid ANSI !!!**
October 19, 2005 at 7:52 am
Hi,
Sorry for wrongly defining problem. I apololize for it.
Let me redefine it again.
The parameter of below SP gets the value as below:
First_name = 'vishal' And last_name = 'Vapi'
Create Procedure XYZ
(
@conditionstring varchar(4096)
)
AS
DECLARE @count int
DECLARE @strQuery varchar(4096)
SET @strQuery = 'Select @count = count(*) from app_client where + @conditionstring
EXEC (@strQuery)
IF @count = 0
BEGIN
SELECT 'Not Found'
END
GO
This gives me Error as "Must Declare the Variable".
October 19, 2005 at 8:06 am
I thought the answer has been given. The error happens during the execution of the dynamic sql !!!!!!!!!!
October 19, 2005 at 8:30 am
Create Procedure XYZ
(
@conditionstring nvarchar(3950)
)
AS
DECLARE @count int
DECLARE @strQuery nvarchar(4000)
SET @strQuery = 'Select @count = count(*) from app_client where ' + @conditionstring
EXEC sp_executesql @strQuery, N'@count int OUTPUT', @count OUTPUT
IF @count = 0
BEGIN
SELECT 'Not Found'
END
GO
Far away is close at hand in the images of elsewhere.
Anon.
October 19, 2005 at 8:32 am
Oh..., those "alluring" traps! [Good one]
I wasn't born stupid - I had to study.
October 19, 2005 at 8:34 am
or
SET @strQuery = 'IF EXISTS(select 1 from app_client where ' + @conditionstring + ') SET @count=1'
to avoid using count(*) to improve performance
Far away is close at hand in the images of elsewhere.
Anon.
October 19, 2005 at 8:43 am
You meant select *? .
October 19, 2005 at 8:47 am
I think he's too "set in his ways"...
**ASCII stupid question, get a stupid ANSI !!!**
October 19, 2005 at 8:51 am
oooops! forgot to initialise the variable
need to add
SET @count = 0
before sp_executesql
Far away is close at hand in the images of elsewhere.
Anon.
October 19, 2005 at 8:53 am
keep getting sidetracked - meant to tell Vishal that if he posts the complete requirements maybe someone'll come up with a solution that does not involve dynamic sql at all...remi hasn't done so yet (or for that matter Farrell) - so I'll post the link to this much traversed path...The curse & blessings of dynamic sql
**ASCII stupid question, get a stupid ANSI !!!**
Viewing 15 posts - 1 through 15 (of 32 total)
You must be logged in to reply to this topic. Login to reply