Showing posts with label temp. Show all posts
Showing posts with label temp. Show all posts

Tuesday, March 27, 2012

Creation of two tables in a stored procedure

Hi,

I was wondering if there was a way to create two temp tables within the same stored procedure.

Can we have two create statements one after the other?

Thanks


Crazy:

I have done so many, many times.

Dave

|||

what exactly error you are getting?

i too did so many number of tmes.

Gurpreet S. Gill

Thursday, March 22, 2012

Creating temp tables in RS

How do I create a simple temp table in RS.SELECT ProductID, Name, StandardCost
INTO #MyProd
FROM Product
GO
SELECT * FROM #MyProd
"Susan" <Susan@.discussions.microsoft.com> wrote in message
news:17C238A5-04F5-40F7-9585-DF47A9B171D5@.microsoft.com...
> How do I create a simple temp table in RS.|||use a stored procedure to create and then select from the temp table.
Paul Turley wrote:
> SELECT ProductID, Name, StandardCost
> INTO #MyProd
> FROM Product
> GO
> SELECT * FROM #MyProd
>
> "Susan" <Susan@.discussions.microsoft.com> wrote in message
> news:17C238A5-04F5-40F7-9585-DF47A9B171D5@.microsoft.com...
> > How do I create a simple temp table in RS.|||Unfortunately RS gives me an error for the use of #.
"Paul Turley" wrote:
> SELECT ProductID, Name, StandardCost
> INTO #MyProd
> FROM Product
> GO
> SELECT * FROM #MyProd
>
> "Susan" <Susan@.discussions.microsoft.com> wrote in message
> news:17C238A5-04F5-40F7-9585-DF47A9B171D5@.microsoft.com...
> > How do I create a simple temp table in RS.
>
>

creating temp tables during login

Frenz,

I need to create tables that are session specific. i.e When I login to a database i have to create a Table that can be accessed across all the procedures and triggers. When I log out the Tables should be droped. I understand that Local Temporary (#) tables can be created in annonymus block, but I need the tables to be created during login.

Your response is highly appreciated.

-CheekuEnsure the TEMPDB has not restricted to a max. size, if so it may fail.
And ensure the server is not stressed with this approach. In general the temp tables will be dropped once the session is closed at the SQL server.
Check books online for more information on temp. tables topic.|||annonymus block

Holy Oracle Batman

And Why do you need to do this?

creating temp table inside SP

I am doing a code review of a stored procedure.
What the procedure does is to create a temp table and then based on parameters sent to
the procedure, call different stored procedures which inserts into the temp table just
created.
Are their any issues with procedures creating temp table on the fly. Any scalability
issues?
There is a good explanation in the BOL. See "DBCC DBREINDEX".
AMB
"rkusenet" wrote:

> I am doing a code review of a stored procedure.
> What the procedure does is to create a temp table and then based on parameters sent to
> the procedure, call different stored procedures which inserts into the temp table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any scalability
> issues?
>
|||Sorry, wrong place.
AMB
"Alejandro Mesa" wrote:
[vbcol=seagreen]
> There is a good explanation in the BOL. See "DBCC DBREINDEX".
>
> AMB
> "rkusenet" wrote:
|||Here's a short article - posted previously by Aaron - on the subject.
http://www.aspfaq.com/show.asp?id=2475
Enjoy.
"rkusenet" <usenet.rk@.gmail.com> wrote in message
news:3cstc2F6q880iU1@.individual.net...
>I am doing a code review of a stored procedure. What the procedure does is
>to create a temp table and then based on parameters sent to
> the procedure, call different stored procedures which inserts into the
> temp table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any
> scalability
> issues?
>
|||"Michael C#" <howsa@.boutdat.com> wrote in message
news:ewjaY42RFHA.1348@.TK2MSFTNGP15.phx.gbl...
> Here's a short article - posted previously by Aaron - on the subject.
> http://www.aspfaq.com/show.asp?id=2475
>
excellent information there.
thanks a lot.

creating temp table inside SP

I am doing a code review of a stored procedure.
What the procedure does is to create a temp table and then based on parameters sent to
the procedure, call different stored procedures which inserts into the temp table just
created.
Are their any issues with procedures creating temp table on the fly. Any scalability
issues?There is a good explanation in the BOL. See "DBCC DBREINDEX".
AMB
"rkusenet" wrote:
> I am doing a code review of a stored procedure.
> What the procedure does is to create a temp table and then based on parameters sent to
> the procedure, call different stored procedures which inserts into the temp table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any scalability
> issues?
>|||Sorry, wrong place.
AMB
"Alejandro Mesa" wrote:
> There is a good explanation in the BOL. See "DBCC DBREINDEX".
>
> AMB
> "rkusenet" wrote:
> > I am doing a code review of a stored procedure.
> > What the procedure does is to create a temp table and then based on parameters sent to
> > the procedure, call different stored procedures which inserts into the temp table just
> > created.
> > Are their any issues with procedures creating temp table on the fly. Any scalability
> > issues?
> >
> >|||Here's a short article - posted previously by Aaron - on the subject.
http://www.aspfaq.com/show.asp?id=2475
Enjoy.
"rkusenet" <usenet.rk@.gmail.com> wrote in message
news:3cstc2F6q880iU1@.individual.net...
>I am doing a code review of a stored procedure. What the procedure does is
>to create a temp table and then based on parameters sent to
> the procedure, call different stored procedures which inserts into the
> temp table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any
> scalability
> issues?
>|||"Michael C#" <howsa@.boutdat.com> wrote in message
news:ewjaY42RFHA.1348@.TK2MSFTNGP15.phx.gbl...
> Here's a short article - posted previously by Aaron - on the subject.
> http://www.aspfaq.com/show.asp?id=2475
>
excellent information there.
thanks a lot.sql

creating temp table inside SP

I am doing a code review of a stored procedure.
What the procedure does is to create a temp table and then based on paramete
rs sent to
the procedure, call different stored procedures which inserts into the temp
table just
created.
Are their any issues with procedures creating temp table on the fly. Any sca
lability
issues?There is a good explanation in the BOL. See "DBCC DBREINDEX".
AMB
"rkusenet" wrote:

> I am doing a code review of a stored procedure.
> What the procedure does is to create a temp table and then based on parame
ters sent to
> the procedure, call different stored procedures which inserts into the tem
p table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any s
calability
> issues?
>|||Sorry, wrong place.
AMB
"Alejandro Mesa" wrote:
[vbcol=seagreen]
> There is a good explanation in the BOL. See "DBCC DBREINDEX".
>
> AMB
> "rkusenet" wrote:
>|||Here's a short article - posted previously by Aaron - on the subject.
http://www.aspfaq.com/show.asp?id=2475
Enjoy.
"rkusenet" <usenet.rk@.gmail.com> wrote in message
news:3cstc2F6q880iU1@.individual.net...
>I am doing a code review of a stored procedure. What the procedure does is
>to create a temp table and then based on parameters sent to
> the procedure, call different stored procedures which inserts into the
> temp table just
> created.
> Are their any issues with procedures creating temp table on the fly. Any
> scalability
> issues?
>|||"Michael C#" <howsa@.boutdat.com> wrote in message
news:ewjaY42RFHA.1348@.TK2MSFTNGP15.phx.gbl...
> Here's a short article - posted previously by Aaron - on the subject.
> http://www.aspfaq.com/show.asp?id=2475
>
excellent information there.
thanks a lot.

Creating temp table in SP - what about indexes ?

I am creating temporary tables in a Stored Procedure, to create paging for a
web frontend.
I do this by creating a temp table with a "identity" field, then copying all
relevant data into the temp table, and then in the end I select out the
actual "page" from the total temp table, ie. from record ID 100 to 150.
During this process, I was wondering if my temp table should have an index,
for optimal performance ?
If my temp table has ie. 5000 records, and I want to select and return only
records from 3500 to 3550, I select with a "where clause" specifying only
records from 3500 to 3550, using the identity field, which automatically
works as a "record counter" for my totalt recordset.
But should I create an index on the identity field, before filling the temp
table with records, and then selecting the actual page to return ?
I mean, if I query using a where clause specifying an column without an
index, wouldnt this create table scans ?
-
Regards,
Tony G.I would think creating the clustered index before filling the table would
give you optimal performance as the data is ordered on insert.
Test each scenario and then you'll be satisfied that you have the best metho
d.
regards,
Mark Baekdal
http://www.dbghost.com
http://www.innovartis.co.uk
+44 (0)208 241 1762
Build, Comparison and Synchronization from Source Control = Database change
management for SQL Server
"Tony Godt" wrote:

> I am creating temporary tables in a Stored Procedure, to create paging for
a
> web frontend.
> I do this by creating a temp table with a "identity" field, then copying a
ll
> relevant data into the temp table, and then in the end I select out the
> actual "page" from the total temp table, ie. from record ID 100 to 150.
> During this process, I was wondering if my temp table should have an index
,
> for optimal performance ?
> If my temp table has ie. 5000 records, and I want to select and return onl
y
> records from 3500 to 3550, I select with a "where clause" specifying only
> records from 3500 to 3550, using the identity field, which automatically
> works as a "record counter" for my totalt recordset.
> But should I create an index on the identity field, before filling the tem
p
> table with records, and then selecting the actual page to return ?
> I mean, if I query using a where clause specifying an column without an
> index, wouldnt this create table scans ?
>
> -
> Regards,
> Tony G.|||> I mean, if I query using a where clause specifying an column without an
> index, wouldn't this create table scans ?
Correct.
I'm a bit through. You say that you create a temp table in the proc
. But how do you re-use
this temp table? I hope that you don't create the temp table, populate it an
d then select from it
each time a user want to display a page?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Tony Godt" <TonyGodt@.discussions.microsoft.com> wrote in message
news:1EFE7BC9-C4A4-4902-84C3-0B79B78A58FA@.microsoft.com...
>I am creating temporary tables in a Stored Procedure, to create paging for
a
> web frontend.
> I do this by creating a temp table with a "identity" field, then copying a
ll
> relevant data into the temp table, and then in the end I select out the
> actual "page" from the total temp table, ie. from record ID 100 to 150.
> During this process, I was wondering if my temp table should have an index
,
> for optimal performance ?
> If my temp table has ie. 5000 records, and I want to select and return onl
y
> records from 3500 to 3550, I select with a "where clause" specifying only
> records from 3500 to 3550, using the identity field, which automatically
> works as a "record counter" for my totalt recordset.
> But should I create an index on the identity field, before filling the tem
p
> table with records, and then selecting the actual page to return ?
> I mean, if I query using a where clause specifying an column without an
> index, wouldnt this create table scans ?
>
> -
> Regards,
> Tony G.

Tuesday, March 20, 2012

creating table when session is invoked

Hi,

When I invoke a session in SQL Server I would to create few temp tables. I have got the queries in a file. Is there any way to invoke these queries (Creating Temp tables) whenever I invoke a session.

Any suggestion/guidence would be greatefully received.

Regards,
SAMThis just smells like a bad idea to me. I don't know of a way to do it with the present generation of SQL Server tools, but I've never really given it any thought.

Can you give me an example of how this would help?

-PatP|||Lets make sure we're all on the same page here. SamCute, can you define what you mean by a "session" in SQL Servers.

Creating table in tempdb using user-defined type

I understand that to create a temp table in tempdb using a user-defined
type, the udt must be defined in Model. However, this doesn't work in
my situation, due to security limitations being placed on the Model
database. (I am using SQL Server 2000).
I'm wondering if there is any viable workaround, short of of using
fixed datatypes when creating the temp table.
For example, say I want to create a temp table as follows:
CREATE TABLE #MyTable(
MyColumn ut_MyColumnType
)
As noted, the above will not work unless ut_MyColumnType is defined in
Model, which can't be done in my situation.
Since I know that ut_MyColumnType is Varchar, I tried using the
COL_LENGTH function to retrieve the size from a column in a non-temp
table that I know is defined using ut_MyColumn type (is there some way
to get the size directly from ut_MyColumnType') into the variable
@.MyColumnLength, and then doing:
CREATE TABLE #MyTable(
MyColumn Varchar(@.MyColumnLength)
)
But this unfortunately doesn't work (Error 170: Incorrect syntax near
'@.MyColumnLength').
Is there any reasonable way to create a temp table using either a
user-defined type not defined in Model, or some substitute method as I
was trying above'IraG wrote:
> I understand that to create a temp table in tempdb using a
> user-defined type, the udt must be defined in Model. However, this
> doesn't work in my situation, due to security limitations being
> placed on the Model database. (I am using SQL Server 2000).
> I'm wondering if there is any viable workaround, short of of using
> fixed datatypes when creating the temp table.
> For example, say I want to create a temp table as follows:
> CREATE TABLE #MyTable(
> MyColumn ut_MyColumnType
> )
> As noted, the above will not work unless ut_MyColumnType is defined in
> Model, which can't be done in my situation.
> Since I know that ut_MyColumnType is Varchar, I tried using the
> COL_LENGTH function to retrieve the size from a column in a non-temp
> table that I know is defined using ut_MyColumn type (is there some way
> to get the size directly from ut_MyColumnType') into the variable
> @.MyColumnLength, and then doing:
> CREATE TABLE #MyTable(
> MyColumn Varchar(@.MyColumnLength)
> )
> But this unfortunately doesn't work (Error 170: Incorrect syntax near
> '@.MyColumnLength').
> Is there any reasonable way to create a temp table using either a
> user-defined type not defined in Model, or some substitute method as I
> was trying above'
You need to use dynamic sql in order to extract the data type and length
of the udt. That's easy enough. The problem is that you are creating a
temp table and because you need to use dynamic sql, the scope of the
temp table is only valid inside the dynamic sql call. Once that call
returns, the temp table is gone. If you were creating a permanent table,
you could do it.
For example, the following fails on the select of the temp table:
exec sp_addtype 'test_type', 'varchar(123)', 'NOT NULL'
go
exec sp_help 'test_type'
create proc dbo.create_temp_table
as
Begin
create table #udt (
[name] varchar(255),
[type] varchar(255),
[length] int,
[precision] int,
[scale] int,
[nullable] varchar(3),
[default_name] varchar(255),
[rule_name] varchar(255),
[collation] varchar(255) )
insert into #udt
exec sp_help 'test_type'
declare @.type varchar(255)
declare @.length int
declare @.new_table nvarchar(1000)
Select TOP 1
@.type = [type],
@.length = [length]
From #UDT
Set @.new_table = 'Create Table #test_table (my_col ' + @.type + '(' +
CAST(@.length as VARCHAR(4)) + '))'
Print @.new_table
Exec (@.new_table)
Select * from #test_table -- fails
Drop table #udt
End
go
exec dbo.create_temp_table
go
exec sp_droptype 'test_type'
go
David Gugick
Imceda Software
www.imceda.com|||Can you create a stored procedure which creates your udt's and mark the proc
as startup
(sp_procoption)?
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"IraG" <ijgla@.clas.ucsb.edu> wrote in message
news:1116459324.112987.289060@.g47g2000cwa.googlegroups.com...
>I understand that to create a temp table in tempdb using a user-defined
> type, the udt must be defined in Model. However, this doesn't work in
> my situation, due to security limitations being placed on the Model
> database. (I am using SQL Server 2000).
> I'm wondering if there is any viable workaround, short of of using
> fixed datatypes when creating the temp table.
> For example, say I want to create a temp table as follows:
> CREATE TABLE #MyTable(
> MyColumn ut_MyColumnType
> )
> As noted, the above will not work unless ut_MyColumnType is defined in
> Model, which can't be done in my situation.
> Since I know that ut_MyColumnType is Varchar, I tried using the
> COL_LENGTH function to retrieve the size from a column in a non-temp
> table that I know is defined using ut_MyColumn type (is there some way
> to get the size directly from ut_MyColumnType') into the variable
> @.MyColumnLength, and then doing:
> CREATE TABLE #MyTable(
> MyColumn Varchar(@.MyColumnLength)
> )
> But this unfortunately doesn't work (Error 170: Incorrect syntax near
> '@.MyColumnLength').
> Is there any reasonable way to create a temp table using either a
> user-defined type not defined in Model, or some substitute method as I
> was trying above'
>|||If you only care about the type, precision and scale and don't want to
preserve rules or defaults in the temp table then you could do it from
an existing column in the current database:
EXEC sp_addtype x_type, 'INTEGER', 'NOT NULL'
GO
CREATE TABLE foo (col x_type)
/* Create a temp table */
SELECT col
INTO #foo
FROM foo
WHERE 1=0
SELECT * FROM #foo
DROP TABLE foo
EXEC sp_droptype x_type
User-defined types are virtually obsolete. In my view there is no good
reason to use them and they come with quite a few divantages. Books
Online recommends using CHECK constraints and DEFAULT constraints
instead and I would suggest you follow that advice if you can. Those
alternatives are more powerful and flexible.
David Portas
SQL Server MVP
--|||David,
Thanks for the great suggestion (SELECT col INTO #foo FROM foo WHERE
1=0). (I would mention that I actually thought of the same thing lying
in bed last night, but I know that nobody would believe me even though
it's true..)
Regarding UDTs being virtually obsolete: where exactly does BOL
recommend using CHECKs and DEFAULTs instead? I'm not clear on how using
these constraints can be used to insure uniformity when defining
columns in different tables.
I realize that UDTs can be a pain when they need to be redefined, but
at least they give you something to get a handle on in that
circumstance. And there is always the hope that future releases of SQL
Server will be more supportive of UDT redefinition.
Would anyone know if SQL Server 2005 offers any better support for UDT
redefinition?|||http://msdn.microsoft.com/library/d...br />
20qb.asp
"Rules are a backward-compatibility feature that perform some of the
same functions as CHECK constraints. CHECK constraints are the
preferred, standard way to restrict the values in a column. CHECK
constraints are also more concise than rules; there can only be one
rule applied to a column, but multiple CHECK constraints can be
applied. CHECK constraints are specified as part of the CREATE TABLE
statement, while rules are created as separate objects and then bound
to the column."
Similar story for Defaults. IMO the advantages of CHECK and DEFAULT
constraints far outweigh the tiny benefit of giving a shorthand name to
a datatype. A data dictionary, good change control procedures and a
quick cut-and-paste between CREATE TABLE statements are a surer way to
guarantee consistency of data elements.
Technically, although Rules and Defaults are deprecated I don't think
Types are (yet) but AFAIK there is no new functionality around them in
2005. Yukon has a new model for UDTs using the CLR.
David Portas
SQL Server MVP
--

Sunday, February 19, 2012

Creating indexes on temp tables in stored procedures

Help,

I have a complex stored procedure (>1000 lines) that uses multiple temp tables with thousands of rows. These temp tables are joined together, and selected from many times.

I tried to improve the performance of the procedure by createing the recommended indexes on my temp tables, but the query plan ignores the indexes and continues to use table scans.

Microsoft SQL Server 2005 - 9.00.3054.00 (Intel X86) Developer Edition (Build 2600: Service Pack 2)

Can you provide some samples of the queries your issuing against these tables?
And maybe some examples of the tables...

|||

The temp tables are used in a cursor (trust me, there's no other way). So these tables of upwards of 200,000 rows each need to be joined, to pull out just a few rows at a time. The tables are fairly simple, around 10 columns each. My issue is that SQL Server doesn't pay attention to indexes created on temp tables because the execution plan is predetermined (I think) and it doesn't take into account an index that doesn't exist at compile-time.

I may have solved my issue by creating another stored procedure that I call from the first. this sub-stored procedure creates indexes on the temp tables (that's all it does) and it looks like the execution plan is no longer doing table scans to join two tables together... it appears to be using the indexes I've created. If you have an easier way to get SQL to use a just-created index, I'm all ears.

FYI: pet peeve of mine

your = something you own (posessive)

you're = conjunction form of "you are"

|||

First off, if you are going to punish people for grammer (yes, that is grammar Smile then you are going to be sorely dissappointed. First off no spell check. Second, no pay. Third, well, come on it is just a bit of help.

Second: the biggest pet peeve of them all around here is not posting your code and DDL so we can look at what you are doing.

Third: "The temp tables are used in a cursor (trust me, there's no other way)." This is rarely true. Almost any cursor can be dealt with in set based code. Some order based accumulations are faster in cursors (or so I have heard Smile, but I haven't written a cursor for a non-system function in years.

You could try adding a hint to your query to force index use. That might work. Try the WITH RECOMPILE hint on the proc too. If nothing else, you can try declaring the cursor in a dynamic SQL call:

declare @.cursorDeclare varchar(max)

set @.cursorDeclare = 'declare bob cursor global for select ''hi'' as hi

open bob'

exec (@.cursorDeclare)

fetch next from bob

That might do the trick. Or, if there are just a few rows to be returned, this might work to spool the dynamic query into a temp table:

create table #tempper

(

value varchar(10)

)

insert into #tempper

exec ('select ''value''')

select *

from #tempper

|||Here, here to the cursor advice; Cursors Are Loathesome.

Creating Global Temp table in sql 2005 DTS

Hi All,
I want to use temp table created in one step to be used in other
step.
How can i do that ?
I tried this..
One step : Create table ##abc (Fileid Int)
Other Step : Select * from ##abc -- When i Parse the query it is
giving error Invalid object name ##abc
Can anyone help me with this ?
Regards,
Rajeev RajputHi
"Rajeev" wrote:

> Hi All,
> I want to use temp table created in one step to be used in other
> step.
> How can i do that ?
>
> I tried this..
>
> One step : Create table ##abc (Fileid Int)
>
> Other Step : Select * from ##abc -- When i Parse the query it is
> giving error Invalid object name ##abc
>
> Can anyone help me with this ?
>
> Regards,
> Rajeev Rajput
>
Assuming that you have set up the correct presedences and the two tasks are
sharing the same connection, then make the RetainSameConnection property to
true on the connection and it should be ok.
John

Creating Global Temp table in sql 2005 DTS

Hi All,
I want to use temp table created in one step to be used in other
step.
How can i do that ?
I tried this..
One step : Create table ##abc (Fileid Int)
Other Step : Select * from ##abc -- When i Parse the query it is
giving error Invalid object name ##abc
Can anyone help me with this ?
Regards,
Rajeev Rajput
Hi
"Rajeev" wrote:

> Hi All,
> I want to use temp table created in one step to be used in other
> step.
> How can i do that ?
>
> I tried this..
>
> One step : Create table ##abc (Fileid Int)
>
> Other Step : Select * from ##abc -- When i Parse the query it is
> giving error Invalid object name ##abc
>
> Can anyone help me with this ?
>
> Regards,
> Rajeev Rajput
>
Assuming that you have set up the correct presedences and the two tasks are
sharing the same connection, then make the RetainSameConnection property to
true on the connection and it should be ok.
John

Creating Global Temp table in sql 2005 DTS

Hi All,
I want to use temp table created in one step to be used in other
step.
How can i do that ?
I tried this..
One step : Create table ##abc (Fileid Int)
Other Step : Select * from ##abc -- When i Parse the query it is
giving error Invalid object name ##abc
Can anyone help me with this ?
Regards,
Rajeev RajputHi
"Rajeev" wrote:
> Hi All,
> I want to use temp table created in one step to be used in other
> step.
> How can i do that ?
>
> I tried this..
>
> One step : Create table ##abc (Fileid Int)
>
> Other Step : Select * from ##abc -- When i Parse the query it is
> giving error Invalid object name ##abc
>
> Can anyone help me with this ?
>
> Regards,
> Rajeev Rajput
>
Assuming that you have set up the correct presedences and the two tasks are
sharing the same connection, then make the RetainSameConnection property to
true on the connection and it should be ok.
John