Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
asifbhura
Posting Yak Master
165 Posts |
Posted - 2010-09-07 : 09:27:23
|
| Hello,I have SP as belowWhen i try to execute it gives me error likeCREATE PROCEDURE [dbo].[ERS_SP_SearchOffer] -- Add the parameters for the stored procedure here @SearchType nvarchar(2), @SearchText nvarchar(100) ASBEGIN SET NOCOUNT ON; DECLARE @ExecQuery nvarchar(100) BEGIN Select @ExecQuery = 'SELECT * FROM ERS_NewOfferOperaion WHERE OfferType=' + @SearchType + ' AND OfferNo=' + @SearchText exec (@ExecQuery) END ENDWhen I execute it like as belowEXEC ERS_SP_SearchOffer '0','KSIEJ040'it gives me error likeMsg 207, Level 16, State 1, Line 1Invalid column name 'KSIEJ040'why it take value as column namePlease help meRegards, |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-07 : 09:39:35
|
| The main code should beSelect @ExecQuery = 'SELECT * FROM ERS_NewOfferOperaion WHERE OfferType=' + @SearchType +' AND OfferNo=''' + @SearchText+''''Also, I dont see any point in using dynamic sql in this caseIt can be simplified toCREATE PROCEDURE [dbo].[ERS_SP_SearchOffer]-- Add the parameters for the stored procedure here@SearchType nvarchar(2),@SearchText nvarchar(100) ASBEGIN SET NOCOUNT ON;BEGINSELECT * FROM ERS_NewOfferOperaion WHERE OfferType=@SearchTypeAND OfferNo= @SearchTextEND ENDMadhivananFailing to plan is Planning to fail |
 |
|
|
asifbhura
Posting Yak Master
165 Posts |
Posted - 2010-09-07 : 09:45:52
|
| thanks it is workingbut i have columnname and value both are dynamically passingHow to achieve this as below i triedALTER PROCEDURE [dbo].[ERS_SP_SearchOfferByDate] -- Add the parameters for the stored procedure here @SearchOption nvarchar(50), @SearchType nvarchar(2), @StartDate nvarchar(100), @EndDate nvarchar(15) ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here DECLARE @ExecQuery nvarchar(100) SET @ExecQuery=@SearchOption -- Insert statements for procedure here if @EndDate <> '' BEGIN select @ExecQuery = 'SELECT * FROM ERS_NewOfferOperation WHERE ' + @SearchOption +'>=' + @StartDate + ' AND' + @SearchOption +'<=' + @EndDate + ' AND OfferType=' + @SearchType Here @SearchOption can be startdate,enddate or RegDateplease how to achieve thisRegards |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-07 : 09:50:18
|
NO DYNAMIC SQL!IF @SearchOption = 'startdate' SELECT * FROM ERS_NewOfferOperation WHERE startDate >= @StartDate AND startDate <= @EndDate AND OfferType = @SearchTypeELSE IF @SearchOption = 'enddate' SELECT * FROM ERS_NewOfferOperation WHERE enddate >= @StartDate AND enddate <= @EndDate AND OfferType = @SearchTypeELSE IF @SearchOption = 'RegDate' SELECT * FROM ERS_NewOfferOperation WHERE RegDate >= @StartDate AND RegDate <= @EndDate AND OfferType = @SearchType N 56°04'39.26"E 12°55'05.63" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2010-09-07 : 09:51:27
|
Or, if you want a better execution planSELECT *FROM ERS_NewOfferOperationWHERE startDate >= @StartDate AND startDate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'startdate'UNION ALLSELECT *FROM ERS_NewOfferOperationWHERE enddate >= @StartDate AND enddate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'enddate'UNION ALLSELECT *FROM ERS_NewOfferOperationWHERE RegDate >= @StartDate AND RegDate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'RegDate' N 56°04'39.26"E 12°55'05.63" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-09-07 : 10:14:37
|
orSELECT *FROM ERS_NewOfferOperationWHERE ( startDate >= @StartDate AND startDate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'startdate')OR( enddate >= @StartDate AND enddate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'enddate')OR( RegDate >= @StartDate AND RegDate <= @EndDate AND OfferType = @SearchType AND @SearchOption = 'RegDate' ) MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|