![]() |
| |||||||
| Sponsored Links |
![]() | « Previous Thread | Next Thread » |
| | LinkBack (1) | Thread Tools | Display Modes |
#1
| ||||
| ||||
| 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 + ' ' +
...
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
| ||||
| ||||
| 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
| ||||
| ||||
| Quote:
, 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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 ' + Your Lord and Master, JMH |
|
#6
| ||||
| ||||
| 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 '
Last edited by Lauramc; March 25th, 2008 at 12:41 PM. Reason: I realized a parentheses was missing. |
|
#7
| ||||
| ||||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| Quote:
(@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
| ||||
| ||||
| 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] |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
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 | |