SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with a stored procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Darkmatter5
Starting Member

17 Posts

Posted - 06/04/2012 :  17:19:43  Show Profile  Reply with Quote
Here's my basic code:


USE [byrndb]
GO

IF OBJECT_ID('spDeleteCounty') IS NOT NULL DROP PROCEDURE spDeleteCounty
GO

CREATE PROC [dbo].[spDeleteCounty]
  @ID int
AS

IF NOT EXISTS (SELECT * FROM surveys WHERE CountyID = @ID)
BEGIN
  DELETE FROM counties WHERE CountyID = @ID
END
ELSE
BEGIN
  DECLARE @CountyName varchar(50) = (SELECT counties.CountyName
                                     FROM surveys
                                     JOIN counties ON counties.CountyID = surveys.CountyID
                                     WHERE surveys.CountyID = @ID)
  DECLARE @Count int = (SELECT counties.CountyName
			FROM surveys
			JOIN counties ON counties.CountyID = surveys.CountyID
			WHERE surveys.CountyID = @ID)
  PRINT 'The selected county of "' & @CountyName & '" is associated with ' & @Count & ' survey(s).  Disassociate the county with those surveys prior to attempting to delete it.'
END


I'm wanting to get two items and output them in a print. When I try and execute I get the following error.

"Msg 402, Level 16, State 1, Procedure spDeleteCounty, Line 20
The data types varchar and varchar are incompatible in the '&' operator."

Any thoughts?

tkizer
Almighty SQL Goddess

USA
35007 Posts

Posted - 06/04/2012 :  17:32:27  Show Profile  Visit tkizer's Homepage  Reply with Quote
Use + instead of & to concatenate strings in T-SQL.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22461 Posts

Posted - 06/05/2012 :  05:26:08  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Why are you printing them? Just return values of both the variables to the client and let it displays the message

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000