Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 23rd, 2005, 04:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting a Jet query to SQL that uses a function

I an trying to two things:

1. Convert a Jet update query to an SQL 2000 update stored procedure.
2. How to simulate a VBA function that was used in that Jet update query.

Here is the Jet update query:
Code:
UPDATE [T_Schedule_BatchItems] 
LEFT JOIN [T_Schedule_ProductionItems] 
  ON [T_Schedule_BatchItems].OrigID = [T_Schedule_ProductionItems].OrigID 
SET [T_Schedule_BatchItems].ItemID = QCntr([T_Schedule_BatchItems].[ItemID],1) 
WHERE [T_Schedule_ProductionItems].OrigID Is Null
 and here is the VBA function:
Code:
Function QCntr(x, Optional Y As Long) As Long        
'Used to count the records in a queary, call SetToZero first to return to 0

        If Y = 1 Then   ' This tells us to start at one or not.
            If IsNull(DMax("[ItemId]", "T-Schedule-ProductionItems")) = False Then
                    If Cntr > DMax("[ItemId]", "T-Schedule-ProductionItems") Then
                       Cntr = Cntr + 1
                       QCntr = Cntr
                    Else
                       Cntr = DMax("[ItemId]", "T-Schedule-ProductionItems")
                       Cntr = Cntr + 1
                       QCntr = Cntr
                    End If
                    Exit Function
            End If
        ElseIf Y = 2 Then  ' put 2 in because we wanted this to run independently for 1 particular query
            If IsNull(DMax("[ItemId]", "T-Schedule-ProductionItems")) = False Then  '*** found items (NOT NULL) start numbering at DMax + 1
                If Cntr > DMax("[ItemId]", "T-Schedule-ProductionItems") Then
                   Cntr = Cntr + 1
                   QCntr = Cntr
                Else
                   Cntr = DMax("[ItemId]", "T-Schedule-ProductionItems")
                   Cntr = Cntr + 1
                   QCntr = Cntr
                End If
            Else   '*** No items found (NULL)  start numbering with 1
                Cntr = Cntr + 1
                QCntr = Cntr
            End If
        Else
            Cntr = Cntr + 1
            QCntr = Cntr
        End If

     End Function
__________________
Mitch
Reply With Quote
  #2 (permalink)  
Old February 23rd, 2005, 11:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you may want to create some user defined function. You can find this information on books Online, Here is a same

Code:
A. Use a SELECT statement with a simple CASE function
Within a SELECT statement, a simple CASE function allows only an equality check; no other comparisons are made. This example uses the CASE function to alter the display of book categories to make them more understandable.

USE pubs
GO
SELECT   Category = 
      CASE type
         WHEN 'popular_comp' THEN 'Popular Computing'
         WHEN 'mod_cook' THEN 'Modern Cooking'
         WHEN 'business' THEN 'Business'
         WHEN 'psychology' THEN 'Psychology'
         WHEN 'trad_cook' THEN 'Traditional Cooking'
         ELSE 'Not yet categorized'
      END,
   CAST(title AS varchar(25)) AS 'Shortened Title',
   price AS Price
FROM titles
WHERE price IS NOT NULL
ORDER BY type, price
COMPUTE AVG(price) BY type
GO
Jaime E. Maccou
Reply With Quote
  #3 (permalink)  
Old February 25th, 2005, 11:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, here is what I have thus far, but I get errors when trying to save it (it says there is an error close to "Case" and "IS") any suggestions?

Code:
CREATE FUNCTION "Function3"
    (
    /*
    @parameter1 datatype = default value,
    @parameter2 datatype
    */
    )
 RETURNS TABLE 
AS
     RETURN 
 (
 /* sql select statement */
CASE
    WHEN Y=1 THEN
        (CASE 
            WHEN (SELECT MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems) IS NULL = False THEN
                (CASE
                    WHEN Cntr>MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems THEN
                        Cntr=Cntr+1
                        QCntr=Cntr
                    Else
                        Cntr>MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems THEN
                        Cntr=Cntr+1
                        QCntr=Cntr
                End)
        END)
    WHEN Y=2 THEN
        (CASE 
            WHEN (SELECT MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems IS NULL) = Flase THEN
                (CASE
                    WHEN Cntr>MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems THEN
                        Cntr=Cntr+1
                        QCntr=Cntr
                    Else
                        Cntr>MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems THEN
                        Cntr=Cntr+1
                        QCntr=Cntr
                End)
        END)
    Else                
        Cntr = Cntr + 1
                QCntr = Cntr
        End
Else
    Cntr = Cntr + 1
    QCntr = Cntr
End
 )
Reply With Quote
  #4 (permalink)  
Old February 25th, 2005, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I modified the above based on some more research, but still gettting syntax errors:

Code:
CREATE FUNCTION "QCntr(@X nvarchar(50), @Y int)"

/*
QCntr(x, Optional Y As Long) As Long:  X will be like E152 and Y will either be 1 or 2
*/
RETURNS int /* datatype */
AS
BEGIN
        /* sql statement ... */
DECLARE @Cntr as int, @QCntr as int, @MaxItem as int
set @Cntr = 0
set @MaxItem = Execute SELECT MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems
If Y=1 
        (If @MaxItem IS NOT NULL  
                (IF @Cntr>@MaxItem 
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr
                    Else
                        @Cntr>@MaxItem 
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr
                End)
        END)
Else IF Y=2 
        (If @MaxItem IS NOT NULL 
                (IF  @Cntr>@MaxItem 
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr
                    Else
                        @Cntr>@MaxItem 
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr
                End)
        END)
    Else                
        @Cntr = @Cntr + 1
                @QCntr = @Cntr
        End
Else
    @Cntr = @Cntr + 1
    @QCntr = @Cntr
End
 )


    RETURN /* value */
END
Reply With Quote
  #5 (permalink)  
Old February 28th, 2005, 10:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I get the same error in an ADP or in SQL Query Analyzer:

Server: Msg 170, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 7
Line 7: Incorrect syntax near 'RETURNS'.
Server: Msg 156, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 13
Incorrect syntax near the keyword 'Execute'.
Server: Msg 156, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 15
Incorrect syntax near the keyword 'If'.
Server: Msg 156, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 16
Incorrect syntax near the keyword 'IF'.
Server: Msg 170, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 17
Line 17: Incorrect syntax near '@Cntr'.
Server: Msg 156, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 26
Incorrect syntax near the keyword 'If'.
Server: Msg 156, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 27
Incorrect syntax near the keyword 'IF'.
Server: Msg 170, Level 15, State 1, Procedure QCntr(@X nvarchar(50), @Y int), Line 28
Line 28: Incorrect syntax near '@Cntr'.
Reply With Quote
  #6 (permalink)  
Old February 28th, 2005, 12:33 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, I've gotten rid of most of the errors, but still get these two:
Code:
Server: Msg 170, Level 15, State 1, Procedure FN_QCntr, Line 24
Line 24: Incorrect syntax near '@Cntr'.
Server: Msg 170, Level 15, State 1, Procedure FN_QCntr, Line 38
Line 38: Incorrect syntax near '@Cntr'.
Here is my updated code:

Code:
CREATE FUNCTION "FN_QCntr"

/*
'Used to count the records in a queary, 
QCntr(x, Optional Y As Long) As Long:  X will be like E152 and Y will either be 1 or 2
*/

(@X nvarchar(50), @Y int)
RETURNS int /* datatype */
AS
BEGIN
        /* sql statement ... */
DECLARE @Cntr as int, @QCntr as int, @MaxItem as int
set @Cntr = 0
set @MaxItem = Go SELECT MAX(ItemID) AS Expr1 FROM T_Schedule_ProductionItems

IF @Y=1


        If @MaxItem IS NOT NULL        --found items (NOT NULL) start numbering at DMax + 1
                --begin
                IF @Cntr > @MaxItem

                        @Cntr=@Cntr+1
                        @QCntr=@Cntr

                    Else
                        @Cntr>@MaxItem
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr




Else IF @Y=2    --  put 2 in because we wanted this to run independently for 1 particular query
        If @MaxItem IS NOT NULL
                IF  @Cntr > @MaxItem
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr
                    Else
                        @Cntr>@MaxItem
                        @Cntr=@Cntr+1
                        @QCntr=@Cntr


    Else        --No items found (NULL)  start numbering with 1
        @Cntr = @Cntr + 1
            @QCntr = @Cntr

Else
    @Cntr = @Cntr + 1
    @QCntr = @Cntr
End
 )


RETURN @QCntr /* value */
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
jet sql query help connect2sandep Access 4 October 28th, 2005 02:16 PM
SQL query dropping parameter function... JackNimble Access 2 March 14th, 2005 04:56 PM
SQL Query - VAL Function itHighway Classic ASP Databases 2 February 23rd, 2005 11:55 PM
sql query VAL function itHighway Classic ASP Basics 1 February 23rd, 2005 11:55 AM
Help converting MySQL SELECT to JET-SQL Alexandre Access 10 May 21st, 2004 02:31 PM



All times are GMT -4. The time now is 11:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.