Wrox Programmer Forums
|
BOOK: Professional SQL Server 2000 Programming
This is the forum to discuss the Wrox book Professional SQL Server 2000 Programming by Robert Vieira; ISBN: 9780764543791
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional SQL Server 2000 Programming 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 July 6th, 2004, 04:14 AM
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default SELECT and local variables

Hi everybody

I am a little confused regarding the code on page 337

    USE Northwind
    DECALRE @Test money
    SELECT @Test = Max(UnitPrice) FROM [Order Details]
    SELECT @Test

Why are there two SELECT statements? I would expect @Test to have a value after the SELECT statement in line 3.

Than you

Ian Hewitt



 
Old July 7th, 2004, 12:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

bc u use @Test = Max(...
why do u use a variable if u dont wanna use that?! its usefull when u dont wanna show the result & then use it somewhere else.
u can do it like:
Code:
USE Northwind
SELECT Max(UnitPrice) FROM [Order Details]
Always:),
Hovik Melkomian.
 
Old July 7th, 2004, 08:21 AM
Registered User
 
Join Date: Jul 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by melvik
 bc u use @Test = Max(...
why do u use a variable if u dont wanna use that?! its usefull when u dont wanna show the result & then use it somewhere else.
u can do it like:
Code:
USE Northwind
SELECT Max(UnitPrice) FROM [Order Details]
Always:),
Hovik Melkomian.
Thanks for the quick response.

I was having a bit of a 'dumb' day yesterday and the question I asked was a result of one of my sprocs not working correctly and getting confused while trying to find the answer. The sproc that caused me the trouble is cut and pasted below :-

CREATE PROCEDURE sp_cClub_New
    @Title as text,
    @Region as integer,
    @Charter as Datetime,
    @NewID Integer OUTPUT
AS
    Declare @Uid_Capitation as integer

    SELECT @Uid_Capitation = ClubDefaultCapitation FROM tblOrgRegion WHERE Uid_Region = @Region


    INSERT INTO tblOrgClub (ClubName, Comment, Uid_Region, DateChartered, Uid_Capitation)
        Values (
            @Title,
            '',
            @Region,
            @Charter,
            @Uid_Capitation
        )
    SELECT @NewId = @@IDENTITY
GO

The problem I was geting when running this sproc via ADO from VB6 was an error message basicly telling me that the Uid_Capitation field of table tblOrgClub could not contain a null value. I then modded the sproc by adding a line after the 'SELECT @Uid_Capitation....." line that so that it read :-

    Declare @Uid_Capitation as integer

    SELECT @Uid_Capitation = ClubDefaultCapitation FROM tblOrgRegion WHERE Uid_Region = @Region
        SELECT @Uid_Capitation

And re-ran the viual basic code. This time it worked correctly. I don't actauly understand why I got the original error message as @Uid_Capitation should have had a valid value by the time the INSERT statement was processed. I am quite sure that the @Region input parameter had a valid value and it should have returned a value of 6. The seconf time I ran the sproc (with the second SELECT statement) and @Uid_Capitation did have the value of 6.

Any thoughts ?

Ian Hewitt





 
Old December 4th, 2004, 07:24 AM
Authorized User
 
Join Date: Nov 2004
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

u can use select and insert into in a single go
insert into x values(select * from y)
or specify the col name that are common
for rest cols u can supply direct values






Similar Threads
Thread Thread Starter Forum Replies Last Post
Concatenating local variables into an execute... BorisG SQL Language 0 September 4th, 2007 09:33 AM
CH. 4 Creating Local Variables mcauliff BOOK: Beginning ASP 3.0 1 March 3rd, 2005 03:57 PM
Variables in Select seeyem General .NET 1 August 30th, 2004 02:41 PM
Set multiple variables with one Select? djuricd SQL Server 2000 2 June 14th, 2004 08:08 AM
Select with 3 variables morpheus SQL Server 2000 3 December 5th, 2003 02:59 PM





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