DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > Microsoft SQL Server

Discuss "Dynamic Stored Procedure" in the Microsoft SQL Server forum.

Microsoft SQL Server - Microsoft SQL Server is a powerful database engine used for large enterprise applications. Learn how to setup, administer and use SQL Server here.


Closed Thread
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-25-2008, 11:58 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default 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
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
Sponsored Links
  #2 (permalink)  
Old 03-25-2008, 12:02 PM
Lauramc's Avatar
SQL Lackey
 
Join Date: Mar 2008
Location: Far Far Away
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Lauramc is an unknown quantity at this point
Default

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

Comments on this post
jmurrayhead agrees: Indeed, this needed to be done
  #3 (permalink)  
Old 03-25-2008, 12:06 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

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
  #4 (permalink)  
Old 03-25-2008, 12:11 PM
Lauramc's Avatar
SQL Lackey
 
Join Date: Mar 2008
Location: Far Far Away
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Lauramc is an unknown quantity at this point
Default

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 (permalink)  
Old 03-25-2008, 12:15 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

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
  #6 (permalink)  
Old 03-25-2008, 12:36 PM
Lauramc's Avatar
SQL Lackey
 
Join Date: Mar 2008
Location: Far Far Away
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Lauramc is an unknown quantity at this point
Default

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; 03-25-2008 at 12:41 PM. Reason: I realized a parentheses was missing.
  #7 (permalink)  
Old 03-25-2008, 12:39 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

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]
  #8 (permalink)  
Old 03-25-2008, 12:49 PM
Lauramc's Avatar
SQL Lackey
 
Join Date: Mar 2008
Location: Far Far Away
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1
Lauramc is an unknown quantity at this point
Default

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 (permalink)  
Old 03-25-2008, 12:50 PM
AOG123's Avatar
Lightning Master

 
Join Date: Mar 2008
Location: Fortress Of Solitude
Posts: 52
Thanks: 3
Thanked 13 Times in 10 Posts
Rep Power: 1
AOG123 is on a distinguished road
Default

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 (permalink)  
Old 03-25-2008, 12:53 PM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 397
Thanks: 12
Thanked 20 Times in 20 Posts
Rep Power: 1
jmurrayhead is on a distinguished road
Default

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]
Closed Thread

  DeveloperBarn Forums > Databases > Microsoft SQL Server

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions on Tables, Stored Procedures, etc. theChris Microsoft SQL Server 2 03-24-2008 11:49 AM

LinkBacks (?)
LinkBack to this Thread: http://www.developerbarn.com/microsoft-sql-server/73-dynamic-stored-procedure.html
Posted By For Type Date
DeveloperBarn Forums - ASP Help, ASP.Net Help, PHP Help, SQL Help, Tutorials, Windows Help This thread Refback 04-26-2008 09:36 AM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.


All times are GMT -4. The time now is 12:57 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
©2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45