Author |
Topic |
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-16 : 06:27:58
|
I have written a Stored Procedure and cant workout where I've gone wrong, this below is the error message and cant see what the problem isMsg 102, Level 15, State 1, Line 9Incorrect syntax near '@StaffID'.This is my codeDECLARE @ContServID VARCHAR(25), @StaffID VARCHAR(10), @SQL VARCHAR(8000)SELECT --@ContServID = 'PHYSIO' @ContServID = Null @StaffID = P% @SQL = 'SELECT ContactServiceID, StaffID ' + 'FROM PARAMED_TEMP ' + 'WHERE StaffID Like (' + @StaffID + ') ' + 'AND ContactServiceID = @ContServID 'PRINT @SQLI'm trying to run a SP where eventually the Variables will be declared in the EXEC Statement when I run itThis is my original SQL where I'm working fromSELECT ContactServiceID, StaffIDFROM PARAMED_TEMPWHERE (StaffID Like 'P%')AND ContactServiceID IS NullWhat have I done wrong or missed? |
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-10-16 : 06:36:37
|
Hi,DECLARE@ContServID VARCHAR(25),@StaffID VARCHAR(10),@SQL VARCHAR(8000)SELECT--@ContServID = 'PHYSIO'@ContServID = Null,@StaffID = 'P%',@SQL = 'SELECT ContactServiceID, StaffID ' +'FROM PARAMED_TEMP ' +'WHERE StaffID Like ''' + @StaffID + ''' ' +'AND ContactServiceID = @ContServID 'PRINT @SQL |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-16 : 07:05:30
|
Excellent Thanks for that, So now that works I've created it as a SP, but get this error when executing the EXEC StatementMsg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'IS'.My SPset ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE [jez].[sp_ZZ]ASDECLARE@ContServID VARCHAR(25),@StaffID VARCHAR(10),@SQL VARCHAR(8000)SELECT @SQL = 'SELECT ContactServiceID, StaffID ' +'FROM PARAMED_TEMP ' +'WHERE StaffID Like ''' + @StaffID + ''' ' +'AND ContactServiceID = @ContServID 'This is the EXEC StatementEXEC sp_ZZ 'P%', IS NULLWhere have I gone wrong with the EXEC Statement? |
|
|
ddamico
Yak Posting Veteran
76 Posts |
Posted - 2008-10-16 : 07:31:09
|
First off, I don't see any input parameters setup to allow you pass those parameters in.CREATE PROCEDURE sp_ZZ (@param1 VARCHAR(10), @param2 VARCHAR(10) )ASBEGINEND1. The DECLARE means you are declaring the variable internally not so you can't pass anything in2. You have to put single quotes around strings especially with spaces3. You should pass in the string 'IS NULL' that should be part of your query in the procedureI think this should solve some of the issues you are seeing. |
|
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-10-16 : 07:47:48
|
CREATE PROCEDURE sp_ZZ (@ContServID VARCHAR(32), @StaffID VARCHAR(32) )ASBEGINdeclare@SQL VARCHAR(8000)Select @sql = 'SELECT ContactServiceID, StaffIDFROM PARAMED_TEMP'declare @str varchar(8000)Select @Str = @Sql + ' where StaffID Like ''' + @StaffID + '''' if @ContServID is not nullSelect @str = @str + 'and ContactServiceID = ''' +@ContServID+''''Exec( @str)ENDExec sp_ZZ 'asa', 'p%' |
|
|
PeterNeo
Constraint Violating Yak Guru
357 Posts |
Posted - 2008-10-16 : 08:13:14
|
U Don't need Dynamic sql for doing this, unless @StaffId is CSVtry this DECLARE @ContServID VARCHAR(25), @StaffID VARCHAR(10), @SQL VARCHAR(8000)SELECT @ContServID = Null @StaffID = 'P%'SELECT ContactServiceID, StaffID FROM PARAMED_TEMP WHERE StaffID Like @StaffID AND (@ContServID IS NULL OR ContactServiceID = @ContServID) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-16 : 08:49:08
|
quote: Originally posted by PeterNeo U Don't need Dynamic sql for doing this, unless @StaffId is CSVtry this DECLARE @ContServID VARCHAR(25), @StaffID VARCHAR(10), @SQL VARCHAR(8000)SELECT @ContServID = Null @StaffID = 'P%'SELECT ContactServiceID, StaffID FROM PARAMED_TEMP WHERE StaffID Like @StaffID AND (@ContServID IS NULL OR ContactServiceID = @ContServID)
SELECT @ContServID = Null @StaffID = 'P%'should beSELECT @ContServID = Null, @StaffID = 'P%'MadhivananFailing to plan is Planning to fail |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-16 : 08:58:51
|
Thanks for that, I've tried it and it brings back to many results...All I want to try and do is this,SELECT ContactServiceID, StaffIDFROM PARAMED_TEMPWHERE (StaffID Like 'P%')AND ContactServiceID IS Nullbut input variables so that the 'P%' can be changed to something else and the Is Null can be is Null or it maybe something else all depending on the EXEC Statement where I want to select the Variables. |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-16 : 09:33:20
|
Using the thread Posted - 10/16/2008 : 08:13:14 I end up with over 11 thousand results and the ContractServiceID doesnt contain just the nulls.If I run the Simple SQL in thread above then it returns about 63 results which is more along the lines for the results I was looking forHow can I amend this SP? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-16 : 13:19:23
|
quote: Originally posted by JezLisle Using the thread Posted - 10/16/2008 : 08:13:14 I end up with over 11 thousand results and the ContractServiceID doesnt contain just the nulls.If I run the Simple SQL in thread above then it returns about 63 results which is more along the lines for the results I was looking forHow can I amend this SP?
do you mean this?set ANSI_NULLS ONset QUOTED_IDENTIFIER ONgoALTER PROCEDURE [jez].[sp_ZZ]@ContServID VARCHAR(25),@StaffID VARCHAR(10)ASSELECT ContactServiceID, StaffID FROM PARAMED_TEMP WHERE StaffID Like @StaffIDAND COALESCE(ContactServiceID,0) = COALESCE(@ContServID,0)GOEXEC Null,'P%' |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-17 : 03:33:43
|
Thanks, I've tried this but it doesnt work as its trying to change, @ContServID is not a bit. This is the error messageMsg 245, Level 16, State 1, Procedure sp_1, Line 8Conversion failed when converting the varchar value 'PHYSIO' to data type int. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-17 : 05:36:54
|
It means StaffID is of INT datatype where you need to pass INT values onlyMadhivananFailing to plan is Planning to fail |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-17 : 05:57:52
|
I'm getting a bit confused here...All I'm looking to do is create a dynamic SQL so that the StaffID can be changed through the EXEC StatementSELECT ContactServiceID, StaffIDFROM PARAMED_TEMPWHERE (StaffID Like 'P%')AND ContactServiceID IS NullThe ContactServiceID will always be NULL, the only thing to change would be the StaffID |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-17 : 06:14:02
|
how will StaffID contain varchar values if its int? |
|
|
JezLisle
Posting Yak Master
132 Posts |
Posted - 2008-10-17 : 06:28:27
|
Heres an example StaffID - P421 thats whay its a VARCHAR |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-10-17 : 08:25:41
|
Post the table structureMadhivananFailing to plan is Planning to fail |
|
|
|