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 |
|
henryvuong
Starting Member
8 Posts |
Posted - 2008-09-26 : 18:21:46
|
| I have a table with the following data:ID Item Price1 World History $15.502 Learning Tennis $9.993 1000 Recipes $18.004 Harry Potter $25.005 C# Programing $15.50I want to write a SQL querry which allows the user to have multiple choices as follow:Choice one: return all itemsChoice two: return item(s) where price = $15.50Choice three: return item(s) where price > $20.00Choice four: return item(s) where price < $20.00I know I can writh multiple querries for the choices, but is it possible to put it all in one querry? |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2008-09-27 : 01:31:13
|
quote: Originally posted by henryvuong I have a table with the following data:ID Item Price1 World History $15.502 Learning Tennis $9.993 1000 Recipes $18.004 Harry Potter $25.005 C# Programing $15.50I want to write a SQL querry which allows the user to have multiple choices as follow:Choice one: return all itemsChoice two: return item(s) where price = $15.50Choice three: return item(s) where price > $20.00Choice four: return item(s) where price < $20.00I know I can writh multiple querries for the choices, but is it possible to put it all in one querry?
DECLARE @t TABLE ( Id INT, BookName VARCHAR(30), Price MONEY)INSERT INTO @tSELECT 1, 'World History', '$15.50' UNION ALLSELECT 2, 'Learning Tennis', '$9.99' UNION ALLSELECT 3, '1000 Recipes', '$18.00' UNION ALLSELECT 4, 'Harry Potter', '$25.00' UNION ALLSELECT 5, 'C# Programing', '$15.50'DECLARE @a CHAR(1)SELECT @a = 'l'SELECT id, bookname,'$'+CAST(price AS VARCHAR(20)) AS Price FROM @t WHERE @a = 'a'UNION ALLSELECT id, bookname,'$'+CAST(price AS VARCHAR(20)) AS Price FROM @t WHERE price = '$15.50' and @a = 'E'UNION ALLSELECT id, bookname,'$'+CAST(price AS VARCHAR(20)) AS Price FROM @t WHERE price > '$20.00' and @a = 'G'UNION ALLSELECT id, bookname,'$'+CAST(price AS VARCHAR(20)) AS Price FROM @t WHERE price < '$20.00' and @a = 'L'If u want return 1.All items Then pass 'A' for @a 2.Equals to '$15.50' then pass 'E' for @a3.Greater than '$20.00' then pass 'G' for @a4.Less than '$20.00' then Pass 'L' for @a |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-27 : 01:40:59
|
quote: Originally posted by henryvuong I have a table with the following data:ID Item Price1 World History $15.502 Learning Tennis $9.993 1000 Recipes $18.004 Harry Potter $25.005 C# Programing $15.50I want to write a SQL querry which allows the user to have multiple choices as follow:Choice one: return all itemsChoice two: return item(s) where price = $15.50Choice three: return item(s) where price > $20.00Choice four: return item(s) where price < $20.00I know I can writh multiple querries for the choices, but is it possible to put it all in one querry?
the best method for this is to create a stored procedure and then call it with whatever values you want to return the specific datalikeCREATE PROC GetItems@Price money=NULL,@Equality char(2)='='ASSELECT *FROM YourTableWHERE (Price=@Price AND @Equality='=')OR (Price >@Price AND @Equality='>')OR (Price <@Price AND @Equality='<')OR @Price IS NULLGO then use it as follows for your scenarios1.EXEC GetItems 2.EXEC GetItems 15.50,'='3.EXEC GetItems 20.00,'>'4.EXEC GetItems 20.00,'<' |
 |
|
|
khasim76
Starting Member
35 Posts |
Posted - 2008-09-29 : 01:44:36
|
| how to create a permanent table for this @t table please let me know |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-29 : 01:59:15
|
quote: Originally posted by khasim76 how to create a permanent table for this @t table please let me know
use CREATE TABLE statement. look into books online for syntax. |
 |
|
|
|
|
|
|
|