Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

T-SQL Help
I am getting the following error message, from the stored procedure below it
.
Can someone tell me how to fix this? I am declaring the variable in question
.

Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@recordCount'.

--If passing in a table name, a property type id must also be passed in.
Alter Procedure  UpdateSiteProperty(@
tableToUse varchar(25) = '',
@propertyTypeID Int = 0,
@clientSiteID Int = 0,
@propertyValue varchar(50) = '') As

Declare @sql varchar(200)
Declare @siteID Int
Declare @count Int
Declare @recordCount Int

--Check to see if a user passed in a table name. If not,
--make sure siteId, propertyTypeID, and propertyValue
--have values other than the defaults.
If @tableToUse = ''
Begin
If @clientSiteID > 0 And @propertyTypeID > 0 And @propertyValue <> ''
Begin
Select @siteID = site_id From sites Where client_site_id = @clientSiteID

If @@Rowcount > 0
Begin
Select * From site_properties Where site_id = @siteID And
property_type_id = @propertyTypeID

--If a record is found, update it. Otherwise insert a record.
If @@Rowcount > 0
Begin
Update site_properties Set property_value = @propertyValue
Where site_id = @siteID And property_type_id = @propertyTypeID
Print 'Updated property for site ' + @clientSiteID
End
Else
Begin
Insert Into site_properties (site_id, property_type_id,
property_value)
Values(@siteID, @propertyTypeID, @propertyValue)
Print 'Inserted property for site ' + @clientSiteID
End
End
End
Else
Begin
print 'If you are not using a table as the source, you must supply
values '
+ 'for client_site_id, property_value, and property_type_id'
End
End
Else
Begin
If @propertyTypeID > 0
Begin
Set @sql = 'Select @recordCount = Count(*) From ' + @tableToUse
Execute (@sql)
Set @count = 1

While @count <= @recordCount
Begin
Set @sql = 'Select @clientSiteID = client_site_id, @propertyValue =
property_value From '
+ @tableToUse + 'Where record_id = @count'
Execute (@sql)
/*
Select @clientSiteID = client_site_id, @propertyValue = property_value
From  site_properties_temp

Where record_id = @count
*/
If @@Rowcount > 0
Begin
Select @siteID = site_id From sites Where client_site_id =
@clientSiteID

If @@Rowcount > 0
Begin
Select * From site_properties Where site_id = @siteID And
property_type_id = @propertyTypeID

--If a record is found, update it. Otherwise insert a record.
If @@Rowcount > 0
Begin
Update site_properties Set property_value = @propertyValue
Where site_id = @siteID And property_type_id = @propertyTypeID
Print 'Updated property for site ' + @clientSiteID
Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
client_site_id = @clientSiteID'
Execute (@sql)
End
Else
Begin
Insert Into site_properties (site_id, property_type_id,
property_value)
Values(@siteID, @propertyTypeID, @propertyValue)
Print 'Inserted property for site ' + @clientSiteID
Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
client_site_id = @clientSiteID'
Execute (@sql)
End
End
End

Set @count = @count + 1
End
End
Else
Begin
Print 'You must pass in the property type id if you pass in a table name.'
End
End



Report this thread to moderator Post Follow-up to this message
Old Post
Mike Collins
09-30-05 01:23 AM


Re: T-SQL Help
Hi,

See the below URL:-

http://www.sommarskog.se/dynamic_sql.html

Thanks
Hari
SQL Server MVP

"Mike Collins" < MikeCollins@discussi
ons.microsoft.com> wrote in message
news:01B0B6D9-D19E-4AC2-B64C- 2062C595D23C@microso
ft.com...
>I am getting the following error message, from the stored procedure below
>it.
> Can someone tell me how to fix this? I am declaring the variable in
> question.
>
> Server: Msg 137, Level 15, State 1, Line 1
> Must declare the variable '@recordCount'.
>
> --If passing in a table name, a property type id must also be passed in.
> Alter Procedure  UpdateSiteProperty(@
tableToUse varchar(25) = '',
> @propertyTypeID Int = 0,
> @clientSiteID Int = 0,
> @propertyValue varchar(50) = '') As
>
> Declare @sql varchar(200)
> Declare @siteID Int
> Declare @count Int
> Declare @recordCount Int
>
> --Check to see if a user passed in a table name. If not,
> --make sure siteId, propertyTypeID, and propertyValue
> --have values other than the defaults.
> If @tableToUse = ''
> Begin
> If @clientSiteID > 0 And @propertyTypeID > 0 And @propertyValue <> ''
> Begin
> Select @siteID = site_id From sites Where client_site_id = @clientSiteID
>
> If @@Rowcount > 0
> Begin
> Select * From site_properties Where site_id = @siteID And
> property_type_id = @propertyTypeID
>
> --If a record is found, update it. Otherwise insert a record.
> If @@Rowcount > 0
> Begin
> Update site_properties Set property_value = @propertyValue
> Where site_id = @siteID And property_type_id = @propertyTypeID
> Print 'Updated property for site ' + @clientSiteID
> End
> Else
> Begin
> Insert Into site_properties (site_id, property_type_id,
> property_value)
> Values(@siteID, @propertyTypeID, @propertyValue)
> Print 'Inserted property for site ' + @clientSiteID
> End
> End
> End
> Else
> Begin
> print 'If you are not using a table as the source, you must supply
> values '
> + 'for client_site_id, property_value, and property_type_id'
> End
> End
> Else
> Begin
> If @propertyTypeID > 0
> Begin
> Set @sql = 'Select @recordCount = Count(*) From ' + @tableToUse
> Execute (@sql)
> Set @count = 1
>
> While @count <= @recordCount
> Begin
> Set @sql = 'Select @clientSiteID = client_site_id, @propertyValue =
> property_value From '
> + @tableToUse + 'Where record_id = @count'
> Execute (@sql)
> /*
> Select @clientSiteID = client_site_id, @propertyValue = property_value
> From  site_properties_temp

> Where record_id = @count
> */
> If @@Rowcount > 0
> Begin
> Select @siteID = site_id From sites Where client_site_id =
> @clientSiteID
>
> If @@Rowcount > 0
> Begin
> Select * From site_properties Where site_id = @siteID And
> property_type_id = @propertyTypeID
>
> --If a record is found, update it. Otherwise insert a record.
> If @@Rowcount > 0
> Begin
> Update site_properties Set property_value = @propertyValue
> Where site_id = @siteID And property_type_id = @propertyTypeID
> Print 'Updated property for site ' + @clientSiteID
> Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
> client_site_id = @clientSiteID'
> Execute (@sql)
> End
> Else
> Begin
> Insert Into site_properties (site_id, property_type_id,
> property_value)
> Values(@siteID, @propertyTypeID, @propertyValue)
> Print 'Inserted property for site ' + @clientSiteID
> Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
> client_site_id = @clientSiteID'
> Execute (@sql)
> End
> End
> End
>
> Set @count = @count + 1
> End
> End
> Else
> Begin
> Print 'You must pass in the property type id if you pass in a table name.'
> End
> End
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Hari Prasad
09-30-05 01:23 AM


RE: T-SQL Help
Mike,

You have to use sp_executesql to use an output parameter with a dynamic sql.

>	Set @sql = 'Select @recordCount = Count(*) From ' + @tableToUse
> 	Execute (@sql)
> 	Set @count = 1
>
> 	While @count <= @recordCount

set @sql = 'select @recordCount = Count(*) From ' +  quotename(@tableToUs
e)

exec sp_executesql @sql, N'@recordCount int output', @recordCount output
...


AMB

"Mike Collins" wrote:

> I am getting the following error message, from the stored procedure below 
it.
> Can someone tell me how to fix this? I am declaring the variable in questi
on.
>
> Server: Msg 137, Level 15, State 1, Line 1
> Must declare the variable '@recordCount'.
>
> --If passing in a table name, a property type id must also be passed in.
> Alter Procedure  UpdateSiteProperty(@
tableToUse varchar(25) = '',
>  																		@p
ropertyTypeID Int = 0,
>  																		@c
lientSiteID Int = 0,
>  																		@p
ropertyValue varchar(50) = '') As
>
> Declare @sql varchar(200)
> Declare @siteID Int
> Declare @count Int
> Declare @recordCount Int
>
> --Check to see if a user passed in a table name. If not,
> --make sure siteId, propertyTypeID, and propertyValue
> --have values other than the defaults.
> If @tableToUse = ''
> 	Begin
> 		If @clientSiteID > 0 And @propertyTypeID > 0 And @propertyValue <> ''
> 			Begin
> 				Select @siteID = site_id From sites Where client_site_id = @clientSite
ID
>
> 				If @@Rowcount > 0
> 					Begin
> 						Select * From site_properties Where site_id = @siteID And
> property_type_id = @propertyTypeID
>
> 						--If a record is found, update it. Otherwise insert a record.
> 						If @@Rowcount > 0
> 							Begin
> 								Update site_properties Set property_value = @propertyValue
> 								Where site_id = @siteID And property_type_id = @propertyTypeID
> 								Print 'Updated property for site ' + @clientSiteID
> 							End
> 						Else
> 							Begin
> 								Insert Into site_properties (site_id, property_type_id,
> property_value)
>  								Values(@site
ID, @propertyTypeID, @propertyValue)
> 								Print 'Inserted property for site ' + @clientSiteID
> 							End
> 					End
> 			End
> 		Else
> 			Begin
> 				print 'If you are not using a table as the source, you must supply
> values '
> 						+ 'for client_site_id, property_value, and property_type_id'
> 			End
> 	End
> Else
> 	Begin
> 		If @propertyTypeID > 0
> 			Begin
> 				Set @sql = 'Select @recordCount = Count(*) From ' + @tableToUse
> 				Execute (@sql)
> 				Set @count = 1
>
> 				While @count <= @recordCount
> 					Begin
> 						Set @sql = 'Select @clientSiteID = client_site_id, @propertyValue =
> property_value From '
> 												+ @tableToUse + 'Where record_id = @count'
> 						Execute (@sql)
> 						/*
> 						Select @clientSiteID = client_site_id, @propertyValue = property_val
ue
> 						From  site_properties_temp

> 						Where record_id = @count
> 						*/
> 						If @@Rowcount > 0
> 							Begin
> 								Select @siteID = site_id From sites Where client_site_id =
> @clientSiteID
>
> 								If @@Rowcount > 0
> 									Begin
> 										Select * From site_properties Where site_id = @siteID And
> property_type_id = @propertyTypeID
>
> 										--If a record is found, update it. Otherwise insert a record.
> 										If @@Rowcount > 0
> 											Begin
> 												Update site_properties Set property_value = @propertyValue
> 												Where site_id = @siteID And property_type_id = @propertyTypeID
> 												Print 'Updated property for site ' + @clientSiteID
> 												Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
> client_site_id = @clientSiteID'
> 												Execute (@sql)
> 											End
> 										Else
> 											Begin
> 												Insert Into site_properties (site_id, property_type_id,
> property_value)
>  												Values(@
siteID, @propertyTypeID, @propertyValue)
> 												Print 'Inserted property for site ' + @clientSiteID
> 												Set @sql = 'Update ' + @tableToUse + 'Set updated = 1 Where
> client_site_id = @clientSiteID'
> 												Execute (@sql)
> 											End
> 									End
> 							End
>
> 						Set @count = @count + 1
> 					End
> 			End
> 		Else
> 			Begin
> 				Print 'You must pass in the property type id if you pass in a table na
me.'
> 			End
> 	End
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Alejandro Mesa
09-30-05 01:23 AM


RE: T-SQL Help
Excellent...thank you very much.

"Alejandro Mesa" wrote:

> Mike,
>
> You have to use sp_executesql to use an output parameter with a dynamic sq
l.
> 
>
> set @sql = 'select @recordCount = Count(*) From ' +  quotename(@tableToUs
e)
>
> exec sp_executesql @sql, N'@recordCount int output', @recordCount output
> ...
>
>
> AMB
>
> "Mike Collins" wrote:
> 

Report this thread to moderator Post Follow-up to this message
Old Post
Mike Collins
09-30-05 08:23 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

MS SQL Server archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 01:38 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006