Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Curiouse View to create for SQLServer 2005


Curiouse View to create for SQLServer 2005

Author
Message
sjimmo
sjimmo
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3351 Visits: 2841
Yes, it was me reading more into it - probably because it happens so often.

Sorry Matt

Steve Jimmo
Sr DBA
“If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
matt32
matt32
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 172
Hi Steve,
no problem , i haven't had the time to write a proper article on how to create a view with Option()
at the end of the statement .. but here is the short way.

1. Create a SP
2. Use Eli's SP to create a View
http://www.sqlservercentral.com/articles/T-SQL/68233/

Sometimes it is useful to use OPTION() to kick the optimizer into the right direction ;-)

This small calendar i used for reporting sale gaps ... nothing is more difficult than reporting data that not exists w00t

Regards Matthias
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
Using OPENROWSET that way is just wrong!!! :-D



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
john.arnott
john.arnott
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1466 Visits: 3059
Paul White NZ (3/30/2010)
Using OPENROWSET that way is just wrong!!! :-D
Uh..... wrong thread, maybe? What OPENROWSET?
matt32
matt32
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 172
the OPENROWSET within Eli Leibas SP to create a view from the output of another SP:-)

http://www.sqlservercentral.com/articles/T-SQL/68233/

this is cool and i use it often
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
matt32 (3/30/2010)
the OPENROWSET within Eli Leibas SP to create a view from the output of another SP:-)
http://www.sqlservercentral.com/articles/T-SQL/68233/
this is cool and i use it often

Let's take a look:

1. The article uses 'allow updates' - which does not even function in 2005 onward
2. Ad Hoc Distributed Queries is required to be ON - this is a security vulnerability and not recommended
4. The view created contains SELECT * - so can never use SCHEMABINDING
5. The view-creating procedure is named sp_
6. The SERVER=. syntax only works for a default instance
7. Object names are SYSNAME = NVARCHAR(128), not VARCHAR(80)

OPENROWSET-specific problems:

Since it is implemented in the query plan as a Remote Scan operator:

1. Fixed cardinality guess of 10,000 rows
2. No statistics are available to the optimizer
3. Destroys the ability of a VIEW to be expanded into the calling statement's execution plan
4. Subject to all the limitations of executing a procedure on a remote, linked server

...I could go on. Still think it is cool?



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
matt32
matt32
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 Visits: 172


1. The article uses 'allow updates' - which does not even function in 2005 onward
2. Ad Hoc Distributed Queries is required to be ON - this is a security vulnerability and not recommended
4. The view created contains SELECT * - so can never use SCHEMABINDING
5. The view-creating procedure is named sp_
6. The SERVER=. syntax only works for a default instance
7. Object names are SYSNAME = NVARCHAR(128), not VARCHAR(80)

OPENROWSET-specific problems:

Since it is implemented in the query plan as a Remote Scan operator:

1. Fixed cardinality guess of 10,000 rows
2. No statistics are available to the optimizer
3. Destroys the ability of a VIEW to be expanded into the calling statement's execution plan
4. Subject to all the limitations of executing a procedure on a remote, linked server

...I could go on. Still think it is cool?

Hi Paul,

for my needs it is a great opportunity to create a view from this sql

WITH mycte AS (SELECT cast ('2007-01-01' AS DATETIME) DateValue UNION ALL SELECT DateValue + 1 FROM mycte WHERE DateValue +
1 < getdate())
SELECT distinct YEAR(DateValue) MyYear,MONTH(DateValue) MyMonth
from mycte OPTION ( MAXRECURSION 0 )

and you shouldn't have a look at all without this context...

ok , Eli Laibas article is not perfect and error free, but who is that - you ?

so all your points will melt to 1 problem :

Ad Hoc Distributed Queries .. i can live with that at my systems

and thats why the solution is smart for my needs.

regards matthias
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10338 Visits: 11350
matt32 (3/31/2010)
for my needs it is a great opportunity to create a view from this sql

WITH mycte AS (SELECT cast ('2007-01-01' AS DATETIME) DateValue UNION ALL SELECT DateValue + 1 FROM mycte WHERE DateValue +
1 < getdate())
SELECT distinct YEAR(DateValue) MyYear,MONTH(DateValue) MyMonth
from mycte OPTION ( MAXRECURSION 0 )

Hey, that's great, but only if performance is not important to you :-)

For a good explanation of why RBAR techniques like this can really suck, see MVP Jeff Moden's article on this site.

For comparison, let's do the same thing, but in a set-based manner:

-- For demonstration purposes only
USE tempdb;
GO
-- Conditional object drops
IF OBJECT_ID(N'dbo.MonthYearList')
IS NOT NULL
DROP FUNCTION dbo.MonthYearList;
GO
IF OBJECT_ID(N'dbo.Numbers')
IS NOT NULL
DROP FUNCTION dbo.Numbers;
GO
-- An in-line function to create a numbers table on the fly
CREATE FUNCTION dbo.Numbers
(@N BIGINT)
RETURNS TABLE
WITH SCHEMABINDING
AS RETURN
WITH
N1 AS (SELECT N = 1 UNION ALL SELECT 1),
N2 AS (SELECT N = 1 FROM N1 T, N1),
N3 AS (SELECT N = 1 FROM N2 T, N2),
N4 AS (SELECT N = 1 FROM N3 T, N3),
N5 AS (SELECT N = 1 FROM N4 T, N4),
N6 AS (SELECT N = 1 FROM N5 T, N5),
NM AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS N FROM N6)
SELECT N
FROM NM
WHERE @N >= 0
AND N <= @N
GO
-- An in-line function wrapping the set-based implementation
CREATE FUNCTION dbo.MonthYearList
(@From DATETIME)
RETURNS TABLE
WITH SCHEMABINDING
AS RETURN
SELECT the_year = YEAR(This.month_start),
the_month = MONTH(This.month_start)
FROM dbo.Numbers (DATEDIFF(MONTH, @From, CURRENT_TIMESTAMP) + 1)
CROSS
APPLY (SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, CURRENT_TIMESTAMP), 0)) Base (month_start)
CROSS
APPLY (SELECT DATEADD(MONTH, 1 - N, Base.month_start)) This (month_start)
WHERE This.month_start BETWEEN @From AND CURRENT_TIMESTAMP;
GO
-- Collect performance statistics
SET STATISTICS IO, TIME ON;

-- The RBAR method
WITH mycte AS (SELECT cast ('1753-01-01' AS DATETIME) DateValue UNION ALL SELECT DateValue + 1 FROM mycte WHERE DateValue +
1 < getdate())
SELECT distinct YEAR(DateValue) MyYear,MONTH(DateValue) MyMonth
from mycte OPTION ( MAXRECURSION 0 )

-- Set based
SELECT *
FROM dbo.MonthYearList ('17530101');

-- Stop recording statistics
SET STATISTICS IO, TIME OFF;
GO
-- Tidy up
DROP FUNCTION dbo.MonthYearList;
DROP FUNCTION dbo.Numbers;



Performance results:
Your method: 2152ms
Set-based: 168ms



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search