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 |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-09 : 01:23:41
|
| Dear Friends,I have the query that are used to fetch the value, I have to check the different Column name for different scenario, So i am forming the Dynamic query based on the user input, but i want to fine tune my query if possible.TableReferenceId | Company | Location | Place | SubLocation | PlaceRegion1001 | 15 | 0 | 51003 | 15 | 4 | 01004 | 14 | 0 | 6I will have value[Not 0] either in Location / Value, And the value will be unique. @ColumnName VARCHAR(30),@Company VARCHAR(20),@Value VARCHAR(20)IF(@ColumnName = 'Company')BEGINSET @StrQuery = 'SELECT ReferenceId FROM ReferenceInfo WHERE ' + ' Company = ' + @Value + ' AND Location = 0 AND Place = 0 AND SubLocation = 0 AND PlaceRegion= 0'END IF(@ColumnName = 'Location')BEGINSET @StrQuery = 'SELECT ReferenceId FROM ReferenceInfo WHERE ' + ' Company = ' + @Company + ' AND Location = ' + @Value + ' AND Place = 0 AND SubLocation = 0 AND PlaceRegion= 0'END IF(@ColumnName = 'Place')BEGINSET @StrQuery = 'SELECT ReferenceId FROM ReferenceInfo WHERE ' + ' Company = ' + @Company + ' AND Location = 0 AND Place = ' + @Value + 'AND SubLocation = 0 AND PlaceRegion= 0'END-- Like the above query i will do for SubLocation and PlaceRegion alsoEXECUTE SP_EXECUTESQL @StrQueryI would like to hear from the experts to fine tune my Stored procedure, Please help me to get a more effective SP, because i may be have more records. |
|
|
Devart
Posting Yak Master
102 Posts |
Posted - 2010-08-09 : 02:40:23
|
| Hello,You can try this:CREATE PROCEDURE <your_procedure_name> (@ColumnName VARCHAR(30), @Company VARCHAR(20), @Value VARCHAR(20)ASSET NOCOUNT ONDECLARE @Location VARCHAR(20)DECLARE @Place VARCHAR(20)DECLARE @SubLocation VARCHAR(20)DECLARE @Region VARCHAR(20)set @Location = '0'set @Place = '0'set @SubLocation = '0'set @Region = '0'IF @ColumnName='Company' SET @Company=@ValueIF @ColumnName='Location' SET @Location=@ValueIF @ColumnName='Place' SET @Place=@ValueIF @ColumnName='SubLocation' SET @SubLocation=@ValueIF @ColumnName='Region' SET @Region=@ValueSELECT ReferenceIdFROM ReferenceInfoWHERE Company = @Company AND Location = @Location AND Place = @Place AND SubLocation = @SubLocation AND PlaceRegion= @PlaceRegionBest Regards,Devart,SQL Server Tools:dbForge Schema ComparedbForge Data ComparedbForge Query Builder |
 |
|
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2010-08-09 : 02:58:59
|
| Hello Devart,Thank you so much, it helped lot.. |
 |
|
|
|
|
|
|
|