T-SQL Question

Results 1 to 2 of 2

Thread: T-SQL Question

  1. #1
    Join Date
    Dec 1969

    Default T-SQL Question

    Is it possible in a T-SQL to declare with @ directives dynamically. For example the stored proecedure will have 2 variables in some run and more on others. <BR><BR>Thanks for any help

  2. #2
    Join Date
    Dec 1969

    Default RE: T-SQL Question

    I&#039;m not sure what you need, but I might show you a trick I use:<BR><BR>CREATE PROCEDURE spSelectSomething<BR>@param1 nvarchar(20) = NULL<BR>@param2 nvarchar(20) = NULL<BR>@param3 nvarchar(20) = NULL<BR>@param4 nvarchar(20) = NULL<BR><BR>AS<BR><BR>SELECT * FROM MyTable WHERE<BR> Field1 = ISNULL(@param1,Field1)<BR> Field2 = ISNULL(@param2,Field2)<BR> Field3 = ISNULL(@param3,Field3)<BR> Field4 = ISNULL(@param4,Field4)<BR><BR>When you don&#039;t pass a certain parameter it gets the default value NULL (because of the = NULL in the declaration part).<BR>Now in the WHERE clause we check if the parameter is null, and if it is NULL compare the field to itself (that&#039;s what the ISNULL function does).<BR><BR>Maybe this is not what you want, but *if* you don&#039;t set a default value to the parameter you *have* to supply it, you can&#039;t do something dynamically.<BR><BR><BR><BR><BR>

Posting Permissions

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