|
Question : creating variables out of column items
|
|
Hello,
Im a newbie and I have been asked to write a procedure in t-SQL for a report in Microsofts Business Development reporting services (2005).
Since security is always a concern, Ive re-written the code to an analogy where I am keeping track of the inventory in Batmans belt from now until the end of the year.
I am not supposed to hard code the items in the database (baterangs, camera, 2 way radio) because there are plans to add more items. (At least the bat tracer, tranquillizer darts and grappling hook will be added in the next year.) They dont want to have to recode the report later when different items become available.
Im currently trying to make a variable equal to a column in a table. This way I can create a drop down list in reporting services of the available items in the column.
If I list the whole path for the tables column ([BatCave\inventory].BatBelt.dbo.resources.resource_type) Im told it is too long. (more than the maximum number of prefixes. The maximum is 3.)
If I try to use the alias a (which I used later in the code for the same table), Sql server does not recognize the alias. (The column prefix 'a' does not match with a table name or alias name used in the query.)
How do I get around this problem?
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO
--alter proc [dbo].[BatBeltResourcesInsufficentStock] --as Declare
@Start_DT datetime, @End_DT datetime, @Res_Type varchar(20)
Set @Start_DT = dateadd(day, 0, getdate()) Set @End_DT = '12/31/2008' Set @Res_Type = a.resource_type
Select distinct a.resource_type as "BatBelt Item", a.description as "Description of Item", max(convert(datetime, b.WkEdDt, 110)) as "Date Supply Ends"
From [BatCave\inventory].BatBelt.dbo.resources a left join resource_template b on a.resource_id = b.resource_id Where(@Res_Type = 'ALL' or a.resource_type = @Res_Type)
group by a.resource_type, a.description , a.resource_id
having isnull(max(convert(datetime, b.WkEdDt,101)), '01/01/1900') <= @End_DT order by a.resource_type, a.description
GO
SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER OFF GO
|
|
Answer : creating variables out of column items
|
|
By the way, @End_DT is the end of next year, not this year.
DISTINCT works on every column in the SELECT statement. You cannot filter on one column (resource type).
The reason that you are continue to see multiple batarangs is because one of the columns in your select statement is different, which makes the row different.
For example, using your earlier column list:
Select distinct a.resource_type as "BatBelt Item", a.description as "Description of Item", max(convert(datetime, b.WkEdDt, 110)) as "Date Supply Ends"
If your table has batarang, 'Batman boomerang', '01/01/08 0000' batarang, 'Batman boomerang', '01/02/08 0000'
You would see both of those because the dates are different (even though all you want is the resource type).
|
|
|
|