Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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 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 February 23rd, 2005, 04:37 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old February 23rd, 2005, 11:59 PM
Friend of Wrox
 
Join Date: Nov 2003
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
 
Old February 25th, 2005, 11:41 AM
Friend of Wrox
 
Join Date: Jun 2003
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
 )
 
Old February 25th, 2005, 12:44 PM
Friend of Wrox
 
Join Date: Jun 2003
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
 
Old February 28th, 2005, 10:28 AM
Friend of Wrox
 
Join Date: Jun 2003
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'.
 
Old February 28th, 2005, 12:33 PM
Friend of Wrox
 
Join Date: Jun 2003
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 */





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





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