webForumDet fria alternativet

CASE i SELECT

5 svar · 487 visningar · startad av nachip

nachipMedlem sedan aug. 200170 inlägg
#1

Jag håller på med en "stored procedure" i SQL Server 2005 som ska hämta produkter i en given kategori.

Proceduren har en paramater som avgör om priset ska visas med eller utan moms. Momssatserna finns sparade i en tabell som heter "Tax" med fälten TaxID, TaxName och TaxValue där den sistnämda är procentsatsen.
Om parametern "@ShowTax" är 1 så ska momssatsen hämtas och läggas till på priserna, annars så ska priserna visas som de är. Antar att detta kan lösas med CASE men jag har inte lyckats klura ut hur. Vore tacksam om någon kunde leda mig i rätt riktning. :)

SQL-satsen i sin nuvarande form ser ut så här:

SELECT 	ROW_NUMBER() OVER (ORDER BY ProductItem.ProductID),
	ProductGroup.ProductGroupID, 
	ProductDescription.ProductName, 
	ProductDescription.ProductShortDescription, 
	ProductPrice.ProductPrice, 
	ProductItem.ProductPriceRetail, 
	ProductItem.ProductPriceSale,
	ProductImage.ImageUrlThumb,
	Manufacturer.ManufacturerID, 
	Manufacturer.ManufacturerName
		
FROM	ProductGroup INNER JOIN
	ProductItem ON ProductGroup.ProductGroupID = ProductItem.ProductGroupID AND ProductItem.ProductDefault = 1 INNER JOIN
	ProductCategoryLink ON ProductGroup.ProductGroupID = ProductCategoryLink.ProductGroupID LEFT JOIN
	ProductPrice ON ProductItem.ProductPriceID = ProductPrice.ProductPriceID LEFT JOIN
	ProductImage ON ProductItem.ProductID = ProductImage.ProductID AND ProductImage.ImageDefault = 1 LEFT JOIN
	Manufacturer ON ProductGroup.ManufacturerID = Manufacturer.ManufacturerID LEFT JOIN
	ProductDescription ON ProductItem.ProductID = ProductDescription.ProductID
		
WHERE	ProductDescription.LanguageID = @LanguageID AND
	ProductCategoryLink.CategoryID = @CategoryID
doggelitoMedlem sedan juni 20003 100 inlägg
#2

nachip skrev:

Vore tacksam om någon kunde leda mig i rätt riktning.

I så fall; hämta alltid priserna utan moms samt att du hämtar med momssatsen.
Sedan adderar du momsen på asp/php-sidan istället. Då kan du använda samma selectsats hela tiden oavsett hur besökaren vill se priserna! :)

nachipMedlem sedan aug. 200170 inlägg
#3

Men är det inte smidigare att göra det i SQL-satsen? Så är det bara att binda resultatet till ett objekt sedan utan att behöva fippla med priset? :) Använder ASP.NET.

Om jag nu vill göra som jag beskrivit. Hur gör jag då? ;)

LarsGMedlem sedan dec. 200012 465 inlägg
#4
ProductPrice.ProductPrice * case 
  when @showTax = 1 then 
   (select 1.0 + taxvalue/100.0
       from tax  
     where taxid = 4711)
  else 1 end
doggelitoMedlem sedan juni 20003 100 inlägg
#5

Jag är inge sql-master men så här kanske! :)
(jag vet inte vad din momskolumn heter)

SELECT 	ROW_NUMBER() OVER (ORDER BY ProductItem.ProductID),
	ProductGroup.ProductGroupID, 
	ProductDescription.ProductName, 
	ProductDescription.ProductShortDescription, 
	CASE @ShowTax WHEN 1 THEN (ProductPrice.ProductPrice+ (ProductPrice.ProductPrice*Din_Moms_kolumn)) ELSE ProductPrice.ProductPrice END, 
	...
nachipMedlem sedan aug. 200170 inlägg
#6

Fungerade perfekt! Tack så mycket! :)

Genererad på 378 ms · cache AV · v20260730165559-full.f96bc7eb