Closed Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17

Thread: Dynamic Stored Procedure

  1. #1
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Dynamic Stored Procedure

    Alright, this is my first stab at a dynamic stored procedure. First off, let me say that I don't get to decide how the database is designed..so that's why the column and table names are horrible.

    I have the following:
    Code:
    DECLARE @sql varchar(255)
    SELECT @sql = 'SELECT candidate_last_name, candidate_first_name... ' +
                         'FROM vw_applicationDetail ' +
                         'WHERE application_status_id = ' + @statusCode + ' ' +
                         ...
    
    I have much more but this is where it errors out. @statusCode is an integer data type. The error:

    Syntax converting the varchar value ''SELECT candidate_last_name, candidate_first_name... FROM vw_applicationDetail WHERE application_status_id = '' to a column of data type int.

    I've tried a variety of ways such as doubling up single quotes around variables and such.

    Any pointers?
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  2. #2
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    209
    Rep Power
    4

    Hey jmh! What is the data type of the @statusCode? If it is not a char or varchar, you have to convert it so it will work in a dynamic procedure. Try CAST(@statuscode as varchar(10)) ... or whatever length you need if 10 is not enough. That should help

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Quote Originally Posted by lauramc View Post
    Hey jmh! What is the data type of the @statusCode? If it is not a char or varchar, you have to convert it so it will work in a dynamic procedure. Try CAST(@statuscode as varchar(10)) ... or whatever length you need if 10 is not enough. That should help
    As stated above , the data type is int. I also tried as you suggested before, but I get the following error:

    Error 403: Invalid operator for data type. Operator equals add, type equals bit.

    Wait...that's for the next line...God I feel stupid...All I had to do was the same thing on my second line and it worked
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  4. #4
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    209
    Rep Power
    4

    Oh good! I am glad you got it sorted. Don't forget that when you execute it you have to do it like so: EXEC(@sql). As a rule I print it while I am testing the string i.e. PRINT @sql. The print will show you what the command looks like so you can tweak it if needed.

  5. #5
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Yeap, I'm still having some issues: Incorrect syntax near 'application_detet'.

    Code:
    DECLARE @sql varchar(255)
    SELECT @sql = 'SELECT candidate_last_name, candidate_first_name... ' +
                         'FROM vw_applicationDetail ' +
                         'WHERE application_status_id = ' + CAST(@statusCode As varchar(10) + ' ' +
                         'AND current_status_ind = ' + CAST(@currentStatus As varchar(10)) + ' '
                         'AND application_deleted_ind = 0 ' + 
    What is your advise, oh wizardess of SQL?

    Your Lord and Master,

    JMH
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  6. #6
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    209
    Rep Power
    4

    You forgot one of the plus signs. Try this:
    Code:
    SELECT @sql = 'SELECT candidate_last_name, candidate_first_name 
                         FROM vw_applicationDetail 
                         WHERE application_status_id = ' + CAST(@statusCode As varchar(10)) +
                         'AND current_status_ind = ' + CAST(@currentStatus As varchar(10)) + ' ' +
                         'AND application_deleted_ind = 0 '
    
    Also you were missing a closed parentheses. I checked this in query analyzer. It works now
    Last edited by Lauramc; March 25th, 2008 at 12:41 PM. Reason: I realized a parentheses was missing.

  7. #7
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    nope, it's there I just forgot to type it in. I can't copy and paste because I have a dev machine and then this machine

    See anything else that might be wrong?

    [EDIT]Again...just a typo...that parentheses is actually there [/EDIT]
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


  8. #8
    I like Data Cubes too... Lauramc has a spectacular aura about Lauramc has a spectacular aura about Lauramc's Avatar
    Join Date
    Mar 2008
    Location
    Far Far Away
    Posts
    209
    Rep Power
    4

    OK try copying my code into your query analyzer. Another thing.. can you give some sample data for your @statusCode and @currentStatus variables? Are you always getting the same error? If so what does it say?

  9. #9
    Lightning Master AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123 is a jewel in the rough AOG123's Avatar
    Join Date
    Mar 2008
    Location
    Fortress Of Solitude
    Posts
    221
    Rep Power
    6

    Quote Originally Posted by Lauramc View Post
    You forgot one of the plus signs. Try this:
    Code:
    SELECT @sql = 'SELECT candidate_last_name, candidate_first_name 
                         FROM vw_applicationDetail 
                         WHERE application_status_id = ' + CAST(@statusCode As varchar(10)) +
                         'AND current_status_ind = ' + CAST(@currentStatus As varchar(10)) + ' ' +
                         'AND application_deleted_ind = 0 '
    
    Also you were missing a closed parentheses. I checked this in query analyzer. It works now
    your missing ' ' + after

    (@statusCode As varchar(10)) +

    Edit: maybe not,.. you had it earlier but took it out?
    If i helped you, make me famous by clicking the

  10. #10
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Washington, D.C.
    Posts
    2,347
    Blog Entries
    9
    Rep Power
    19

    Can't copy and paste...code is on a dev machine. @statusCode could be 99, @currentStatus is a bit (comes in from .Net app as True or False), @applicationType could be 4.

    Again, the error is: Line 1: Incorrect syntax near 'applcation_delet'

    [EDIT]@AOG - that was just a typo[/EDIT]
    jmurrayhead
    If you agree, give me rep. If my post helped you, click "Thanks".
    If you like it here...throw us a few bones to help support us.


Closed Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Permissions on Tables, Stored Procedures, etc.
    By theChris in forum Microsoft SQL Server
    Replies: 2
    Last Post: March 24th, 2008, 11:49 AM

Bookmarks

Posting Permissions

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

SEO by vBSEO