Results 1 to 7 of 7

Thread: SQL Server

  1. #1
    Join Date
    Mar 2004
    Posts
    2

    Unanswered: SQL Server

    declare @param varchar(20)
    set @param ='Exports'
    select * from Gv_Division where 1=1 + 'and Div_Name=@param '


    I have Written this query in query Anlyzer and executed
    I Got an error like this
    'Syntax error converting the varchar value 'and Div_Name=@param ' to a column of data type int.'



    My main Intention is Building Dynamically where Condition In the query for search criteria

    Please Give the Solution for this

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    Try something like this one:

    declare @param varchar(20)
    set @param ='Exports'

    exec('select * from Gv_Division where 1=1 and Div_Name='''+@param+ '''')

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Code:
    declare @param varchar(20), @sql varchar(8000)
    set @param ='Exports'
    select @sql = 'select * from Gv_Division where 1=1 + and Div_Name='+ '''' + @param + ''''
    SELECT @sql
    EXEC(@sql)
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  4. #4
    Join Date
    Jul 2003
    Location
    San Antonio, TX
    Posts
    3,662
    I'd rather not. Can you come up with reasonable justification to base your back-end functionality fully on dynamic SQL?

    It's like buying a Lambargini and picking random people from the street as your drivers...Would you do it? "Pick me, pick me!!!"

    How about rethinking your design? And your app architecture for that matter?

  5. #5
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    Originally posted by rdjabarov
    I'd rather not. Can you come up with reasonable justification to base your back-end functionality fully on dynamic SQL?

    It's like buying a Lambargini and picking random people from the street as your drivers...Would you do it? "Pick me, pick me!!!"

    How about rethinking your design? And your app architecture for that matter?
    rethinking? It is always a good idea!

  6. #6
    Join Date
    Mar 2004
    Posts
    2

    Thank you

    Originally posted by Brett Kaiser
    Code:
    
    But i don't need like this 
    I want to add Dynamically where condition to the original Query
    i don't use the following statements
    select @sql = 'select * from Gv_Division where 1=1 + and Div_Name='+ '''' + @param + ''''
    SELECT @sql
    EXEC(@sql)
    
    
    is it Possible?
    If Possible try to give me the solution
    Thank you Very much
    
    declare @param varchar(20), @sql varchar(8000)
    set @param ='Exports'
    select @sql = 'select * from Gv_Division where 1=1 + and Div_Name='+ '''' + @param + ''''
    SELECT @sql
    EXEC(@sql)
    Last edited by Ranjani; 03-06-04 at 05:00.

  7. #7
    Join Date
    Feb 2004
    Location
    New Jersey
    Posts
    17
    What Brett suggested should work.If that is not what you need,then make it more clear what exactly you are looking for.Then only someone can take a look at it.
    Cinil

Posting Permissions

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