Go Back   DeveloperBarn Forums > Databases > Microsoft SQL Server

Sponsored Links

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 « Previous Thread | Next Thread »
 
LinkBack (1) Thread Tools Display Modes
  1 links from elsewhere to this Post. Click to view. #1  
Old March 25th, 2008, 11:58 AM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

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
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Sponsored Links
  #2  
Old March 25th, 2008, 12:02 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

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  
Old March 25th, 2008, 12:06 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

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  
Old March 25th, 2008, 12:11 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

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  
Old March 25th, 2008, 12:15 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

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  
Old March 25th, 2008, 12:36 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

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; March 25th, 2008 at 12:41 PM. Reason: I realized a parentheses was missing.
  #7  
Old March 25th, 2008, 12:39 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

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  
Old March 25th, 2008, 12:49 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

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  
Old March 25th, 2008, 12:50 PM
AOG123's Avatar
Lightning Master

 
Join Date: Mar 2008
Location: Fortress Of Solitude
Posts: 93
Thanks: 6
Thanked 23 Times in 18 Posts
Rep Power: 1
AOG123 is on a distinguished road

Awards Showcase
Microsoft Access 
Total Awards: 1

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  
Old March 25th, 2008, 12:53 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 820
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft SQL Server Microsoft Windows Microsoft .Net Classic ASP 
Total Awards: 4

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions on Tables, Stored Procedures, etc. theChris Microsoft SQL Server 2 March 24th, 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 April 26th, 2008 09:36 AM


All times are GMT -4. The time now is 05:28 PM.



Content Relevant URLs by vBSEO 3.2.0