| Author |
Topic |
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-28 : 19:24:13
|
| Here is my stored procedureset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[Sp_PriceReport] @usname varchar(100),@name varchar(50)asbegin SELECT MobID, MAX(CASE WHEN low = 1 THEN Merchant ELSE NULL END) AS LowPriceMerchant, MAX(CASE WHEN low = 1 THEN Price ELSE NULL END) AS LowPrice, MAX(CASE WHEN high = 1 THEN Merchant ELSE NULL END) AS HighPriceMerchant, MAX(CASE WHEN high = 1 THEN Price ELSE NULL END) AS HighPrice, MAX(CASE WHEN My = 1 THEN Price ELSE NULL END) AS MyPriceINTO ##TempFROM ( SELECT MobID, Merchant, Price, ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price DESC) AS high, ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price) AS low, CASE Merchant WHEN @usname THEN 1 ELSE 0 END AS My FROM tbl_'+@name+'_master_merchant ) AS tGROUP BY MobIDHAVING MAX(My) = 1SELECT DISTINCT t1.MPN_SKU, t1.Product_Name, m.MyPrice, m.LowPrice, m.LowPriceMerchant, m.HighPrice, m.HighPriceMerchant FROM ##Temp AS mINNER JOIN tbl_'+@name+'_master_product AS t1 ON t1.MobID = m.MobIDdrop table ##tempendWhile executing this i got this errorMsg 102, Level 15, State 1, Procedure Sp_PriceReport, Line 27Incorrect syntax near '+@name+'.Msg 102, Level 15, State 1, Procedure Sp_PriceReport, Line 41Incorrect syntax near '+@name+'. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-28 : 19:41:36
|
| There are 9 tables like tbl_wilson_master_merchant,tbl_wilson_master_product,tbl_jafry_master_merchant,tbl_jafry_master_product....So i need for a dynamic query to solve this problem.. |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-28 : 19:42:55
|
| Actually I created tbl_master_merchant... But now I need like this .. Pls help me .. |
 |
|
|
kevlangdo
Starting Member
5 Posts |
Posted - 2010-09-28 : 21:32:18
|
| Maybe you could try retrieving the table from sys.tables or sys.objects and assigning it to a Type as Table.Then use the table variableKevin LanguedocSr BI Developerwww.kcodebook.com/wp |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
rohitvishwakarma
Posting Yak Master
232 Posts |
Posted - 2010-09-29 : 04:27:32
|
here it is:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[Sp_PriceReport]@usname varchar(100),@name varchar(50)asbegin DECLARE @sql VARCHAR(1000)SET @sql = 'SELECT MobID,MAX(CASE WHEN low = 1 THEN Merchant ELSE NULL END) AS LowPriceMerchant,MAX(CASE WHEN low = 1 THEN Price ELSE NULL END) AS LowPrice,MAX(CASE WHEN high = 1 THEN Merchant ELSE NULL END) AS HighPriceMerchant,MAX(CASE WHEN high = 1 THEN Price ELSE NULL END) AS HighPrice,MAX(CASE WHEN My = 1 THEN Price ELSE NULL END) AS MyPriceINTO ##TempFROM (SELECT MobID,Merchant,Price,ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price DESC) AS high,ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price) AS low,CASE MerchantWHEN @usname THEN 1ELSE 0END AS MyFROM tbl_' + @name + '_master_merchant) AS tGROUP BY MobIDHAVING MAX(My) = 1SELECT DISTINCT t1.MPN_SKU,t1.Product_Name,m.MyPrice,m.LowPrice,m.LowPriceMerchant,m.HighPrice,m.HighPriceMerchantFROM ##Temp AS mINNER JOIN tbl_'+@name+'_master_product AS t1 ON t1.MobID = m.MobIDdrop table ##temp'end |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 13:30:41
|
| Hey rohit, I got this errorMsg 137, Level 15, State 2, Line 16Must declare the scalar variable "@usname". |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 13:35:11
|
| How to assign value to usname |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:00:32
|
| I tried this WHEN '+@usname+' THEN 1 and got this error Msg 207, Level 16, State 1, Line 16Invalid column name 'teptronics'. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2010-09-29 : 14:06:47
|
| teptronics isn't in the code that he posted, so please post the code that you've now got.Also, please do not use sp_ as the prefix for stored procedures. That is a performance problem as SQL automatically will check the master database first for the object. Never use sp_ is my recommendation, although other people will say you can use it for user-created system objects. I avoid sp_ like the plague!Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:09:23
|
| exec [Sp_PriceReport] 'teptronics','shopzilla' ... This way I executed my SP |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:10:56
|
| Thanks for your advice ..Hereafter I will keep it in mind when creating procedures |
 |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:12:15
|
| He missed one line before end statement EXEC(@sql) |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:24:08
|
| These are the columns names idMOBIDBottomline_PriceConditionFeatured_StoreFree_ShippingHacker_SafeLogoMerchantMerchant_RatingPriceRank_After_SortingRank_Before_SortingShippingStore_LikeTaxZip_Def_RankZip_Sorted_RankUpdated_DateUpdated_TimeCrawl_SourceReviews |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:27:46
|
| set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER procedure [dbo].[Sp_PriceReport2] @usname varchar(100)asbegin SELECT MobID, MAX(CASE WHEN low = 1 THEN Merchant ELSE NULL END) AS LowPriceMerchant, MAX(CASE WHEN low = 1 THEN Price ELSE NULL END) AS LowPrice, MAX(CASE WHEN high = 1 THEN Merchant ELSE NULL END) AS HighPriceMerchant, MAX(CASE WHEN high = 1 THEN Price ELSE NULL END) AS HighPrice, MAX(CASE WHEN My = 1 THEN Price ELSE NULL END) AS MyPriceINTO ##TempFROM ( SELECT MobID, Merchant, Price, ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price DESC) AS high, ROW_NUMBER() OVER (PARTITION BY MobID ORDER BY Price) AS low, CASE Merchant WHEN @usname THEN 1 ELSE 0 END AS My FROM tbl_master_merchant ) AS tGROUP BY MobIDHAVING MAX(My) = 1SELECT DISTINCT t1.MPN_SKU, t1.Product_Name, m.MyPrice, m.LowPrice, m.LowPriceMerchant, m.HighPrice, m.HighPriceMerchant FROM ##Temp AS mINNER JOIN tbl_master_product AS t1 ON t1.MobID = m.MobIDdrop table ##tempendThis is my actual SP..It generates my expected output .. My need is to use the same sp for other 9 CSE so i created dynamic query for creating table names dynamically.. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jafrywilson
Constraint Violating Yak Guru
379 Posts |
Posted - 2010-09-29 : 14:37:53
|
| I already tried this WHEN '+@usname+' THEN 1 and got this error Msg 207, Level 16, State 1, Line 16Invalid column name 'teptronics'.Because it is inside of the dynamic query ..What can we do ? to solve this |
 |
|
|
Next Page
|