Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 17th, 2005, 10:30 AM
Registered User
 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help with query please

Hello. The code below is part of a larger stored procedure to generate record IDs. This portion of the code is giving the following error message:
    Server: Msg 170, Level 15, State 1, Line 11

Code:
DECLARE @id_int int
DECLARE @id_text varchar(13)
DECLARE @id_temp varchar(13)
DECLARE @CurrentYr int

SET @CurrentYr = convert(int, year(getdate()))

SELECT @id_temp = vchRoutingID, 
case 
    when @CurrentYr = intCurrentYr then
    @id_int = CONVERT(INT,SUBSTRING(vchRoutingID,8,DATALENGTH(vchRoutingID)-5))
    when @CurrentYr > intCurrentYr then @id_int = 0 end
FROM tblRoutingID WITH (TABLOCKX,HOLDLOCK)
Individually, the lines work. So what might the problem be? Any help will be greatly appreciated. Thanks.
 
Old June 17th, 2005, 10:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by dkspivey
Individually, the lines work. So what might the problem be?
I seriously doubt the CASE lines work.

In SQL, CASE ... END is an expression which returns a value; it is not a procedural construct with code in it like assignments.

Your SELECT should read something like:
Code:
SELECT @id_temp = vchRoutingID,
@id_int = 
    CASE 
        WHEN @CurrentYr = intCurrentYr THEN
            CONVERT(INT,SUBSTRING(vchRoutingID,8,DATALENGTH(vchRoutingID)-5))
        WHEN @CurrentYr > intCurrentYr THEN
            0
    END
FROM ...
Note also that you are using a SELECT from a table to assign local variables and this will fail if the SELECT returns more than one row ...



Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old June 17th, 2005, 11:31 AM
Registered User
 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Jeff!

Thanks so much for your help. The procedure works.

You're absolutely right about the CASE statement. What I should have said was that the lines worked if I removed the CASE statement.

Thanks again, Donna :D





Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.