Groups | Blog | Home
all groups > sql server programming > february 2004 >

sql server programming : Help creating a view ?


Rich Dillon
2/13/2004 9:10:08 PM
Rob,

CREATE VIEW TrickView AS
SELECT b.Column2 AS Trick
FROM TestOut AS a, TestOut AS b
WHERE a.Column1='OptionSetting' AND a.Column2='Y' AND b.Column1='Trick'

SELECT *
FROM TrickView;

UPDATE TestOut
SET Column2 = 'N'
WHERE Column1='OptionSetting';

SELECT *
FROM TrickView;


This assumes, of course, that there will never be more than one row in your
table with Column1 = 'OptionSetting' and that only 'Y' and 'N' will be
permitted Column2 values for that row. Additionally, it's worth pointing
out that using one column with one datatype to represent potentially several
entirely different kinds of things depending on the value of the other
column can be very problematic; data integrity is just too hard to ensure.
Is this just a for-fun problem?

Hope that helps,
Rich

[quoted text, click to view]

rob
2/13/2004 10:34:31 PM
/*
Any help creating a view that would do the following...

If OptionSetting = "Y" then return a 1 column table with column
name Trick and show all values associated with Trick (i.e., A,B,C)

If OptionSetting = "N" then return a 1 column table with column
name Trick with no results

Thank you
*/


CREATE TABLE [dbo].[TestOut] (
[Column1] [varchar] (50) NULL ,
[Column2] [varchar] (50) NULL
) ON [PRIMARY]

INSERT INTO TestOut Values ('OptionSetting' , 'Y')
INSERT INTO TestOut Values ('Trick' , 'A')
INSERT INTO TestOut Values ('Trick' , 'B')
INSERT INTO TestOut Values ('Trick' , 'C')


rob
2/14/2004 12:21:58 AM
Thanks Greg,

I did get this to work using nested views so long as there was only 1 Trick
(i.e., by creating a crosstab type view 2 columns... Trick and
OptionSetting, followed by a view that specified OptionSetting = Y in the
criteria). It failed when I added another Trick row...

Nested views would be OK as well...

As a first step, I thought a 2 column view could be produced returning....

Trick OptionSetting
A Y
B Y
C Y

Then perform a view on this set that returned where OptionSetting = Y

Likewise, the flipside

Trick OptionSetting
A N
B N
C N

Then perform a view on this set that returned where OptionSetting = N

I've seen folks in this group do just about everything using "set theory"
and thought I'd give it a try.

I was dealt the table as a given, it was definitely not my choice.

Thanks,
Rob



[quoted text, click to view]

Steve Kass
2/14/2004 1:51:57 AM
Rob,

Is this what you want? I don't really understand what this is
supposed to help with, though:

CREATE TABLE [dbo].[TestOut] (
[Column1] [varchar] (50) NULL ,
[Column2] [varchar] (50) NULL
) ON [PRIMARY]

INSERT INTO TestOut Values ('OptionSetting' , 'Y')
INSERT INTO TestOut Values ('Trick' , 'A')
INSERT INTO TestOut Values ('Trick' , 'B')
INSERT INTO TestOut Values ('Trick' , 'C')
GO

CREATE VIEW TestV AS
SELECT
Column2 AS Trick
FROM TestOut
WHERE Column1 = 'Trick'
AND NOT EXISTS (
SELECT * FROM TestOut
WHERE Column1 = 'OptionSetting'
AND Column2 <> 'Y'
)
GO

SELECT * FROM TestV
GO

UPDATE TestOut SET
Column2 = 'N'
WHERE Column1 = 'OptionSetting'
GO

SELECT * FROM TestV

GO
DROP VIEW TestV
DROP TABLE TestOut

SK

[quoted text, click to view]
rob
2/14/2004 10:48:56 AM
Thanks Rich and Steve...


Why someone chose to put both the "switch" and the "values" related to the
switch in one table is another story...

However this view is used to exclude rows from yet another view.... as you
can see both the values to exclude as well as whether or not to even apply
the rule is all in the same table.





[quoted text, click to view]

Greg Linwood
2/14/2004 3:18:24 PM
Hi Rob.

What do you mean by "OptionSetting"? How would you pass the option to the
view?

Views don't take parameters - that's what stored procedures & functions are
for. Which of these is best for you will likely depend on how you want to
pass the parameter - either in procedural logic or within a select
statement.

HTH

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Joe Celko
2/16/2004 8:43:28 AM
A VIEW is a virutal table; a table has columns of known, fixed
datatypes. In a good DB, the view has a name that describes waht it
models. Your monster will have a name like
"Could_be_Apples_or_Bagpipes" instead.

It is not a badly designed stored procedure. A good stored procedure
has properties called "Cohension", which should be strong, and
"Coupling", which should be weak.

We can probably kludge soemthing with dynamic SQL, but that ain't your
real problem.

This is an ACCESS nightmare ported to SQL Server, isn't it? That why
you have no keys and VARRCHAR(50) columns to invite bad data.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button