Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Temp Tables revisited Expand / Collapse
Author
Message
Posted Saturday, June 22, 2013 12:33 PM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
Comments posted to this topic are about the item Temp Tables revisited
Post #1466479
Posted Sunday, June 23, 2013 10:15 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, October 28, 2014 6:04 AM
Points: 1,127, Visits: 1,599
Good question on how parsing works.....very important to know the internals well.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1466567
Posted Sunday, June 23, 2013 11:21 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, February 6, 2014 4:15 AM
Points: 1,242, Visits: 1,546
Good basic question. But unfortunately, i have selected wrong one in hurry



Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com

Post #1466579
Posted Monday, June 24, 2013 12:09 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, December 14, 2014 11:09 PM
Points: 1,962, Visits: 2,406
Danny Ocean (6/23/2013)
Good basic question. But unfortunately, i have selected wrong one in hurry

+1



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1466588
Posted Monday, June 24, 2013 12:34 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 10:49 AM
Points: 1,194, Visits: 791
what could be the better approach in this kind of situation , is there any better option than this

DECLARE @MoreColumns bit;
SET @MoreColumns = 1

Create table #table( id int )
IF @MoreColumns = 1
alter table #table
add col1 varchar(10)
ELSE
alter table #table
add col2 varchar(10)
DROP TABLE #Table


Post #1466593
Posted Monday, June 24, 2013 12:39 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, December 10, 2014 10:49 AM
Points: 1,194, Visits: 791
http://support.microsoft.com/kb/295305
Post #1466595
Posted Monday, June 24, 2013 12:50 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:56 AM
Points: 2,842, Visits: 3,876
Why was this question reposted with the same misleading explanation as the original QOTD a month ago?

Best Regards,
Chris Büttner
Post #1466602
Posted Monday, June 24, 2013 1:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 9:20 AM
Points: 1,372, Visits: 1,567
sharath.chalamgari (6/24/2013)
http://support.microsoft.com/kb/295305


I never thought about creating a temp table using if/else statements. Seems this is a good way. Thanks for sharing

If that was not a temp table, we can use EXEC ('CREATE TABLE <>....') as well.


~ Lokesh Vij

Guidelines for quicker answers on T-SQL question
Guidelines for answers on Performance questions

Link to my Blog Post --> www.SQLPathy.com

Follow me @Twitter

Post #1466609
Posted Monday, June 24, 2013 1:50 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:54 PM
Points: 6,130, Visits: 8,394
Good question, and mostly correct explanation. Except for the last sentence.

Such an error breaks the integrity of the code and makes further evaluation impossible. Parsing stops immediately at this point and the DROP statement is never evaluated.

This is not true. Parsing does continue. The reason the DROP TABLE does not generate an error is because of "defered name resolution" - if a table does not exist at parse time, it is still accepted and SQL Server will continue parsing and compiling the batch, making a mental note to retry parsing and compiling the offending statement at execution time. This is in order to support scenario's where a table is created while the code is running (e.g. a permanent table created by a called stored procedure, or by <shudder> dynamic SQL).

One way to check this is to change DROP TABLE to DROP TALE - you will now get a third error message, that TALE is not a supported object type for CREATE, DROP, or ALTER. Another way to see this in action is to execute this batch:
SELECT 1;
DROP TABLE #TableA;

If you run this, you'll get a result set with "1", and an error message - this indicates that the error was only returned after the initial parse and compile; executing the first select; and then the second attempt to parse and compile the DROP statement. Conversely, the batch below will ONLY return an error message, indicating that even the first statement of the batch was not executed because the error was generated during the initial parse and compile:
SELECT 1;
IF 1 = 1
CREATE TABLE #Table (A int);
ELSE
CREATE TABLE #Table (B int);




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1466615
Posted Monday, June 24, 2013 1:57 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:54 PM
Points: 6,130, Visits: 8,394
sharath.chalamgari (6/24/2013)
what could be the better approach in this kind of situation , is there any better option than this

Rethink your design.

Seriously. SQL is not designed for tables that can contain just about everything. If you start your process with a solid data model, you'll get a solid database design. In that design, each table has a fixed set of columns that will not change at run-time. So in a well designed database, you should not need this kind of nonsense.

The only situations where I have seen this kind of code are:
1. No solid design, or a "design" (and I'm using that word in a very loose sense) that allows end users to add columns at run time. That's not what the relational model is for. Use relational tables for structured data, and if the user has a need for additional unstructrured data, provide them with an XML column for that. There may be other alternatives, to be decided on a case by case basis, but the bottom line is that "normal" tables should be fixed and only change during deployment, and flexible schema should be in its own separate area.
2. Generic purpose stored procedures that do "something" for generic tables. Just don't. How hard can it be to make multiple copies of the stored procedure and adapt them as needed to the specifics of each table? I much rather have twenty-five almost identical stored procedures that are elementary to graps and simple to maintain, than one "generic" stored proc that needs so much control flow to handle all special cases that it will become a maintenance nightmare the minute the original developer moves on.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1466617
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse