|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Today @ 3:43 AM
Points: 139,
Visits: 215
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 1:39 PM
Points: 1,355,
Visits: 1,740
|
|
| Thanks for the heads-up on the potential metadata problem. I haven't stumbled on this problem yet, but it would have only been a matter of time. Clever work-around!
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 2:54 AM
Points: 23,
Visits: 97
|
|
| Good to know, and the reverse will also be useful! When you have to use a front end tool such as Excel or Access, which does not use FMTONLY, temporarily adding it to the procedure will allow building a query without waiting for full execution.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 11:33 AM
Points: 4,
Visits: 43
|
|
This is also useful to know when using the wizard in Visual Studio 2005 for creating Typed data Sets.
The ugly work around is to create a table and select at the top of the stored procedure just to satisfy the tool, then comment it out and un-comment the realy code after that.
This looks like it would be a better way. And its something I always meant to trace using profiler and never had time. Thanks.
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 1:35 AM
Points: 4,789,
Visits: 1,336
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, March 17, 2011 10:15 PM
Points: 40,
Visits: 30
|
|
Renato, this is a very useful and interesting article. Can you tell us how this code from your example works? It looks like you are evaluating the status of the FMTONLY option by doing a count from sys.filegroups. I don't understand why this would return the session status of FMTONLY.
CREATE PROCEDURE dbo.ExportGraphicIndex AS BEGIN DECLARE @FmtOnlyTest int; -- If this is null then FMTONLY was ON SELECT @FmtOnlyTest = count(*) FROM sys.filegroups; IF @FmtOnlyTest IS NULL -- FMTONLY was ON SET FMTONLY OFF;
Andy Hilliard Owl Creek Consulting
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Monday, January 14, 2013 2:54 AM
Points: 23,
Visits: 97
|
|
If FMTONLY is ON, all queries return zero rows, so a variable assignment in any select has no effect, and the variable keeps its previous value, which is NULL if it was never initialized. Here is another example which works in SQL2000, but at any rate you can query any table you have permission on.
DECLARE @FmtOnlyTest int set @FmtOnlyTest = 0 SELECT count(*) AS C FROM dbo.sysobjects SELECT @FmtOnlyTest = count(*) FROM dbo.sysobjects IF ( @FmtOnlyTest = 0 ) BEGIN SET FMTONLY OFF PRINT 'NOW OFF' END
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Today @ 3:43 AM
Points: 139,
Visits: 215
|
|
Hi Andy,
Thomas Keller is absolutely right and explained it very well.
I chose SELECT COUNT(*) because that aggregate function will normally always return a row with a non-null value. That means that I could be sure that if the variable is null, the query did not return any rows, and FMTONLY was set.
The code is improved if @SetFmtOnly is explicitly initialised (as in Thomas's example)
Renato Buda
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:54 PM
Points: 525,
Visits: 617
|
|
Thank you, this is excellent tip. Where were you 6 months ago when I spent days working around this limitation??!!!
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 11:56 PM
Points: 328,
Visits: 125
|
|
Excellent topic!
I just want to add one thing. If you use table variable or tmp table use all column names instead of just * in select statement.
Forexample:
Does not Work select * from BLA_BLA
Use select col1, col1,... from BLA_BLA
|
|
|
|