Results 1 to 7 of 7

Thread: Testing whether a SQL statement is valid - SQL Server 2000

Threaded View

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Testing whether a SQL statement is valid - SQL Server 2000

    I have a bit of an odd problem. Within our SQL Server 2000 database, we have a table that holds default values for given IDs. However, in some circumstances, the default value held in the table will actually be a SQL command. For example:
    Code:
    DatePart(YYYY, GetDate())
    Currently, this is all handled within the VB application that calls the SP that looks at this table; within a Try Catch block, it tries to run the SQL command, and if it returns a value rather than an error, it uses the returned value rather than literally putting the text of "DatePart(YYYY, GetDate())" as the value.

    The VB code looks like this, where RunSQLGetString is a function that runs the SQL against the database:
    Code:
                            Dim DefaultValue As String
                            Try
                                DefaultValue = RunSQLGetString("Select " & Attribute.DefaultValue)
                            Catch ex As Exception
                                DefaultValue = Attribute.DefaultValue
                            End Try
    I would rather handle this in the SQL stored procedure, but I'm not sure of the best way to go about it. I really don't like the idea of using error handling for coding purposes, and besides, as it's SQL 2000, I'm limited to using @@Error:
    Code:
        Select @Default = IsNull(DefaultValue,'') From Attributes Where AttrID = @AttrID
        If IsNull (@Default, '') <> ''
            Begin
                Insert Into #Converted
                Execute ('Select ' + @Default)
                If @@Error = 0
                    Begin
                        Set @Default = (Select Top 1 Converted From #Converted)
                    End
                Select  @AttrValue  =   @Default
            End
    Can anyone see a tidier way around this?
    Last edited by InvisibleDuncan; May 17th, 2013 at 10:24 AM. Reason: Pressed Enter too early
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width