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

Wednesday, March 21, 2012

Location of logging for temp tables

I have a stored procedure that creates a temp table that is used to
insert data from a dynamic query. My question is does the logging of
the insert occur in the local database running the stored procedure or
does it occur in the tempdb database? My log file for local database
is growing significantly and I want to rule out that the temp table is
not writing to the local database log. I have done some testing and it
appears that is not but I just confirm my findings."Travis" <Travi424@.hotmail.com> wrote in message
news:1126279238.782948.112340@.z14g2000cwz.googlegr oups.com...
>I have a stored procedure that creates a temp table that is used to
> insert data from a dynamic query. My question is does the logging of
> the insert occur in the local database running the stored procedure or
> does it occur in the tempdb database? My log file for local database
> is growing significantly and I want to rule out that the temp table is
> not writing to the local database log. I have done some testing and it
> appears that is not but I just confirm my findings.

An INSERT into the temp table will be logged in tempdb; an INSERT from the
temp table into a permanent table will be logged in that database.

Simon|||Thanks!

Friday, March 9, 2012

Local Temporary Tables

I have created a local temporary table (#Temp) and placed data in it.

When I do:
SELECT * #Temp

The result is a set of rows with no columns.

The SELECT statement is in the same procedure as the creation of #Temp; so I
don't believe this is a scoping issue. Does that make sense?

Thank you in advance,
EricBeringer wrote:
> I have created a local temporary table (#Temp) and placed data in it.
> When I do:
> SELECT * #Temp
> The result is a set of rows with no columns.
> The SELECT statement is in the same procedure as the creation of #Temp; so I
> don't believe this is a scoping issue. Does that make sense?
> Thank you in advance,
> Eric

No, it doesn't make sense. First, your code above is invalid SQL.
Second, you don't provide any DDL or sample data for us to reproduce the
problem.

Zach|||Please be patient with me. I'm learning how to do this stuff by myelf and
just begining! :)

At anyrate here is an example:
ALTER PROCEDURE proc1

AS

SET NOCOUNT ON

CREATE TABLE #TempTable(my_text CHAR(10))

INSERT INTO #TempTable(my_text) VALUES ('test')

INSERT INTO #TempTable(my_text) VALUES ('test2')

INSERT INTO #TempTable(my_text) VALUES ('test3')

SELECT * FROM #TempTable

The result (visually in Access 2002, in datasheet view) after executing the
procedure is simply a row header with three rows and no columns. Of note,
if the SET NOCOUNT ON is commented out there is nothing.

Thanks again,

Eric

"nib" <individual_news@.nibsworld.com> wrote in message
news:2tqnlcF21lmapU2@.uni-berlin.de...
> Beringer wrote:
>> I have created a local temporary table (#Temp) and placed data in it.
>>
>> When I do:
>> SELECT * #Temp
>>
>> The result is a set of rows with no columns.
>>
>> The SELECT statement is in the same procedure as the creation of #Temp;
>> so I don't believe this is a scoping issue. Does that make sense?
>>
>> Thank you in advance,
>> Eric
>>
>>
> No, it doesn't make sense. First, your code above is invalid SQL. Second,
> you don't provide any DDL or sample data for us to reproduce the problem.
> Zach|||I do similar stored procedures like this all the time. Except I create the
SP via Enterprise Manager and Check Syntax etc . Have you tried executing
this from Query Analyzer just to see if it works? Or just create a stored
procedure in Enterprise Manager and EXECUTE it from Query Analyzer...

The column in your example should have one column heading and three rows of
data (if I am reading it correctly).

Is it generating any errors?

Barry

"Beringer" <borden_eric@.invalid.com> wrote in message
news:l1Xdd.56381$kz3.16039@.fed1read02...
> Please be patient with me. I'm learning how to do this stuff by myelf and
> just begining! :)
> At anyrate here is an example:
> ALTER PROCEDURE proc1
> AS
> SET NOCOUNT ON
> CREATE TABLE #TempTable(my_text CHAR(10))
> INSERT INTO #TempTable(my_text) VALUES ('test')
> INSERT INTO #TempTable(my_text) VALUES ('test2')
> INSERT INTO #TempTable(my_text) VALUES ('test3')
> SELECT * FROM #TempTable
> The result (visually in Access 2002, in datasheet view) after executing
> the procedure is simply a row header with three rows and no columns. Of
> note, if the SET NOCOUNT ON is commented out there is nothing.
> Thanks again,
> Eric
> "nib" <individual_news@.nibsworld.com> wrote in message
> news:2tqnlcF21lmapU2@.uni-berlin.de...
>> Beringer wrote:
>>> I have created a local temporary table (#Temp) and placed data in it.
>>>
>>> When I do:
>>> SELECT * #Temp
>>>
>>> The result is a set of rows with no columns.
>>>
>>> The SELECT statement is in the same procedure as the creation of #Temp;
>>> so I don't believe this is a scoping issue. Does that make sense?
>>>
>>> Thank you in advance,
>>> Eric
>>>
>>>
>>
>> No, it doesn't make sense. First, your code above is invalid SQL. Second,
>> you don't provide any DDL or sample data for us to reproduce the problem.
>>
>> Zach

Local temporary table schema

It seems odd that you would need to do this, since your code for the current
connection had to know how to create the #Temp Table.
However, if you must, you could use this as a starting point:
SELECT *
FROM Tempdb..sysobjects
WHERE name LIKE '%#Temp%'
SELECT *
FROM Tempdb..syscolumns
WHERE id = object_ID('tempdb..#Temp')
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-0754-45EB-8978-6391D4
939CD1@.microsoft.com...
> Is there a way to get the schema (columns) of a local temporary table in S
QL
> Server 2005? TEMPDBO.INFORMATION_SCHEMA.COLUMNS lists the columns, but if
> multiple connections use the same temp table name, there doesn't seem to b
e a
> way to differentiate between them. Each table name is appended with a bunc
h
> of underscores and a random number (i.e.
> " #MyTable________________________________
_________________________________
________________________________________
_0000000002B0"),
> but that doesn't seem to help much.
>
> Thanks,
>
> Rich WoodIs there a way to get the schema (columns) of a local temporary table in SQL
Server 2005? TEMPDBO.INFORMATION_SCHEMA.COLUMNS lists the columns, but if
multiple connections use the same temp table name, there doesn't seem to be
a
way to differentiate between them. Each table name is appended with a bunch
of underscores and a random number (i.e.
" #MyTable________________________________
___________________________________
_______________________________________0
000000002B0"),
but that doesn't seem to help much.
Thanks,
Rich Wood|||It seems odd that you would need to do this, since your code for the current
connection had to know how to create the #Temp Table.
However, if you must, you could use this as a starting point:
SELECT *
FROM Tempdb..sysobjects
WHERE name LIKE '%#Temp%'
SELECT *
FROM Tempdb..syscolumns
WHERE id = object_ID('tempdb..#Temp')
--
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-0754-45EB-8978-6391D4
939CD1@.microsoft.com...
> Is there a way to get the schema (columns) of a local temporary table in S
QL
> Server 2005? TEMPDBO.INFORMATION_SCHEMA.COLUMNS lists the columns, but if
> multiple connections use the same temp table name, there doesn't seem to b
e a
> way to differentiate between them. Each table name is appended with a bunc
h
> of underscores and a random number (i.e.
> " #MyTable________________________________
_________________________________
________________________________________
_0000000002B0"),
> but that doesn't seem to help much.
>
> Thanks,
>
> Rich Wood|||Arnie Rowland wrote:

> It seems odd that you would need to do this, since your code for the curr=
ent connection had to know how to create the #Temp Table.
> However, if you must, you could use this as a starting point:
> SELECT *
> FROM Tempdb..sysobjects
> WHERE name LIKE '%#Temp%'
> SELECT *
> FROM Tempdb..syscolumns
> WHERE id =3D object_ID('tempdb..#Temp')
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-07=
54-45EB-8978-6391D4939CD1@.microsoft.com...
n SQL[vbcol=seagreen]
if[vbcol=seagreen]
o be a[vbcol=seagreen]
unch[vbcol=seagreen]
________________________________________
____0000000002B0"),[vbcol=seagreen]
> --=3D_NextPart_000_01D9_01C69935.4E112260
> Content-Type: text/html; charset=3DUtf-8
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 2562
> =EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
> <META content=3D"MSHTML 6.00.5296.0" name=3DGENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY>
> <DIV><FONT face=3DArial size=3D2>It seems odd that you would need to do t=
his, since
> your code for the current connection had to know how to create the #Temp
> Table.</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>However, if you must, you could use this=
as a
> starting point:</FONT></DIV>
> <DIV><BR><FONT face=3D"Courier New" size=3D2>SELECT * </FONT></DIV>
> <DIV><FONT face=3D"Courier New" size=3D2>FROM Tempdb..sysobje=

cts
> </FONT></DIV>
> <DIV><FONT face=3D"Courier New" size=3D2>WHERE name LIKE '%#Temp%'</FONT>=
</DIV><FONT
> face=3D"Courier New" size=3D2>
> <DIV><BR>SELECT * </DIV>
> <DIV>FROM Tempdb..syscolumns </DIV>
> <DIV>WHERE id =3D object_ID('tempdb..#Temp')</FONT></DIV>
> <DIV><BR><FONT face=3DArial size=3D2>-- <BR>Arnie Rowland, YACE* <BR>"To =
be
> successful, your heart must accompany your knowledge."</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>*Yet Another certification Exam</FONT></=
DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>"Rich Wood" <</FONT><A
> href=3D"mailto:RichWood@.newsgroup.nospam"><FONT face=3DArial
> size=3D2>RichWood@.newsgroup.nospam</FONT></A><FONT face=3DArial size=3D2>=
> wrote in
> message </FONT><A
> href=3D"news:ECDEB660-0754-45EB-8978-6391D4939CD1@.microsoft.com"><FONT fa=
ce=3DArial
> size=3D2>news:ECDEB660-0754-45EB-8978-6391D4939CD1@.microsoft.com</FONT></=
A><FONT
> face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>> Is=
there a way to
> get the schema (columns) of a local temporary table in SQL <BR>> Serve=
r 2005?
> TEMPDBO.INFORMATION_SCHEMA.COLUMNS lists the columns, but if <BR>> mul=
tiple
> connections use the same temp table name, there doesn't seem to be a <BR>=
>
> way to differentiate between them. Each table name is appended with a bun=
ch
> <BR>> of underscores and a random number (i.e. <BR>>
> " #MyTable________________________________
________________________________=
________________________________________
__0000000002B0"),
> <BR>> but that doesn't seem to help much.<BR>> <BR>> Thanks,<BR>=
>
> <BR>> Rich Wood</FONT></BODY></HTML>
> --=3D_NextPart_000_01D9_01C69935.4E112260--
use=20
sp_help 'tempdb..#temp'
Regards
Amish Shah|||Hi Rich,
Thank you for your posting!
You could use the object_ID function to get the object id of the temporary
table you current user created.
As Arnie mentioned, you could use the following statement to query the
information.
SELECT *
FROM Tempdb..sysobjects
WHERE id = object_ID('tempdb..#TEMP')
SELECT *
FROM Tempdb..syscolumns
WHERE id = object_ID('tempdb..#TEMP')
Hope this will be helpful!
Sincerely,
Wei Lu
Microsoft Online Community Support
========================================
==========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
==========
This posting is provided "AS IS" with no warranties, and confers no rights.|||To make a long story short, the local temporary table is created dynamically
with the result of a query -- so even though I create it locally I don't kno
w
the column names at design time.
Thanks for your response -- using the object_id function to query the
tempdb..syscolumns table worked.
Rich Wood
"Arnie Rowland" wrote:
[vbcol=seagreen]
> It seems odd that you would need to do this, since your code for the curre
nt connection had to know how to create the #Temp Table.
> However, if you must, you could use this as a starting point:
> SELECT *
> FROM Tempdb..sysobjects
> WHERE name LIKE '%#Temp%'
> SELECT *
> FROM Tempdb..syscolumns
> WHERE id = object_ID('tempdb..#Temp')
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-075
4-45EB-8978-6391D4939CD1@.microsoft.com...|||Arnie Rowland wrote:

> It seems odd that you would need to do this, since your code for the curr=
ent connection had to know how to create the #Temp Table.
> However, if you must, you could use this as a starting point:
> SELECT *
> FROM Tempdb..sysobjects
> WHERE name LIKE '%#Temp%'
> SELECT *
> FROM Tempdb..syscolumns
> WHERE id =3D object_ID('tempdb..#Temp')
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-07=
54-45EB-8978-6391D4939CD1@.microsoft.com...
n SQL[vbcol=seagreen]
if[vbcol=seagreen]
o be a[vbcol=seagreen]
unch[vbcol=seagreen]
________________________________________
____0000000002B0"),[vbcol=seagreen]
> --=3D_NextPart_000_01D9_01C69935.4E112260
> Content-Type: text/html; charset=3DUtf-8
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 2562
> =EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
> <META content=3D"MSHTML 6.00.5296.0" name=3DGENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY>
> <DIV><FONT face=3DArial size=3D2>It seems odd that you would need to do t=
his, since
> your code for the current connection had to know how to create the #Temp
> Table.</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>However, if you must, you could use this=
as a
> starting point:</FONT></DIV>
> <DIV><BR><FONT face=3D"Courier New" size=3D2>SELECT * </FONT></DIV>
> <DIV><FONT face=3D"Courier New" size=3D2>FROM Tempdb..sysobje=

cts
> </FONT></DIV>
> <DIV><FONT face=3D"Courier New" size=3D2>WHERE name LIKE '%#Temp%'</FONT>=
</DIV><FONT
> face=3D"Courier New" size=3D2>
> <DIV><BR>SELECT * </DIV>
> <DIV>FROM Tempdb..syscolumns </DIV>
> <DIV>WHERE id =3D object_ID('tempdb..#Temp')</FONT></DIV>
> <DIV><BR><FONT face=3DArial size=3D2>-- <BR>Arnie Rowland, YACE* <BR>"To =
be
> successful, your heart must accompany your knowledge."</FONT></DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>*Yet Another certification Exam</FONT></=
DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2></FONT> </DIV>
> <DIV><FONT face=3DArial size=3D2>"Rich Wood" <</FONT><A
> href=3D"mailto:RichWood@.newsgroup.nospam"><FONT face=3DArial
> size=3D2>RichWood@.newsgroup.nospam</FONT></A><FONT face=3DArial size=3D2>=
> wrote in
> message </FONT><A
> href=3D"news:ECDEB660-0754-45EB-8978-6391D4939CD1@.microsoft.com"><FONT fa=
ce=3DArial
> size=3D2>news:ECDEB660-0754-45EB-8978-6391D4939CD1@.microsoft.com</FONT></=
A><FONT
> face=3DArial size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>> Is=
there a way to
> get the schema (columns) of a local temporary table in SQL <BR>> Serve=
r 2005?
> TEMPDBO.INFORMATION_SCHEMA.COLUMNS lists the columns, but if <BR>> mul=
tiple
> connections use the same temp table name, there doesn't seem to be a <BR>=
>
> way to differentiate between them. Each table name is appended with a bun=
ch
> <BR>> of underscores and a random number (i.e. <BR>>
> " #MyTable________________________________
________________________________=
________________________________________
__0000000002B0"),
> <BR>> but that doesn't seem to help much.<BR>> <BR>> Thanks,<BR>=
>
> <BR>> Rich Wood</FONT></BODY></HTML>
> --=3D_NextPart_000_01D9_01C69935.4E112260--
use=20
sp_help 'tempdb..#temp'
Regards
Amish Shah|||Hi Rich,
Thank you for your posting!
You could use the object_ID function to get the object id of the temporary
table you current user created.
As Arnie mentioned, you could use the following statement to query the
information.
SELECT *
FROM Tempdb..sysobjects
WHERE id = object_ID('tempdb..#TEMP')
SELECT *
FROM Tempdb..syscolumns
WHERE id = object_ID('tempdb..#TEMP')
Hope this will be helpful!
Sincerely,
Wei Lu
Microsoft Online Community Support
========================================
==========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
==========
This posting is provided "AS IS" with no warranties, and confers no rights.|||To make a long story short, the local temporary table is created dynamically
with the result of a query -- so even though I create it locally I don't kno
w
the column names at design time.
Thanks for your response -- using the object_id function to query the
tempdb..syscolumns table worked.
Rich Wood
"Arnie Rowland" wrote:
[vbcol=seagreen]
> It seems odd that you would need to do this, since your code for the curre
nt connection had to know how to create the #Temp Table.
> However, if you must, you could use this as a starting point:
> SELECT *
> FROM Tempdb..sysobjects
> WHERE name LIKE '%#Temp%'
> SELECT *
> FROM Tempdb..syscolumns
> WHERE id = object_ID('tempdb..#Temp')
> --
> Arnie Rowland, YACE*
> "To be successful, your heart must accompany your knowledge."
> *Yet Another certification Exam
>
> "Rich Wood" <RichWood@.newsgroup.nospam> wrote in message news:ECDEB660-075
4-45EB-8978-6391D4939CD1@.microsoft.com...

Local Temp tables versus Global

I have a SP that dynamicly constructs a sql phrase that is executed with the
execute keyword correctly.
One of the variables is the name of a Global temporary table created on the
fly as follows
Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as varchar(2) )
+ Cast(DATEPART (millisecond , getdate()) as varchar(3) )
Set @.lstr_sTableName = 'dbo.##CC' + Rtrim(@.sCustomerNo) + @.strUniqueTable
Then I execute the following
EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
@.lstr_sTableName + " " + @.sFromWhereStart )
I had some concurrency issues and wanted to switch to Local Temporary Table,
so as soon as I take off one of the '#' signs, execute ther SP I get the
following error :
---
Server: Msg 208, Level 16, State 1, Line 1
Invalid object name '#CCOI154315_61466018841ML160'.
----
When re-putting the second # sign, the SP works like a charm
Al I missing something regarding local temporary tables
Thanks in advanceHi
The creation of the table is not in the same scope as the executed SELECT
statement. You may want to execute them together.
John
"SalamElias" <eliassal@.online.nospam> wrote in message
news:2A3F3044-D1E7-4FCB-9896-C18EEC95A207@.microsoft.com...
>I have a SP that dynamicly constructs a sql phrase that is executed with
>the
> execute keyword correctly.
> One of the variables is the name of a Global temporary table created on
> the
> fly as follows
> Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as
> varchar(2) )
> + Cast(DATEPART (millisecond , getdate()) as varchar(3) )
> Set @.lstr_sTableName = 'dbo.##CC' + Rtrim(@.sCustomerNo) + @.strUniqueTable
> Then I execute the following
> EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
> @.lstr_sTableName + " " + @.sFromWhereStart )
>
> I had some concurrency issues and wanted to switch to Local Temporary
> Table,
> so as soon as I take off one of the '#' signs, execute ther SP I get the
> following error :
> ---
> Server: Msg 208, Level 16, State 1, Line 1
> Invalid object name '#CCOI154315_61466018841ML160'.
> ----
> When re-putting the second # sign, the SP works like a charm
> Al I missing something regarding local temporary tables
> Thanks in advance|||Thansk for your response. As I said they are in the same SP.
Can you please explain what do you mean by ' not in the same scope '.
As I understand from what is indicated in BOL that nested SP in SP can call
local temporary tables :
--
A local temporary table created in a stored procedure is dropped
automatically when the stored procedure completes. The table can be
referenced by any nested stored procedures executed by the stored procedure
that created the table. The table cannot be referenced by the process which
called the stored procedure that created the table.
--
My understanding is that second level sp can execute sql phrases against LTT
in hosting SP, No?
"John Bell" wrote:

> Hi
> The creation of the table is not in the same scope as the executed SELECT
> statement. You may want to execute them together.
> John
> "SalamElias" <eliassal@.online.nospam> wrote in message
> news:2A3F3044-D1E7-4FCB-9896-C18EEC95A207@.microsoft.com...
>
>|||One last thing I forgot to add, how do you execute both in the same scope?
can youn please show me?
Thansk in advance
"John Bell" wrote:

> Hi
> The creation of the table is not in the same scope as the executed SELECT
> statement. You may want to execute them together.
> John
> "SalamElias" <eliassal@.online.nospam> wrote in message
> news:2A3F3044-D1E7-4FCB-9896-C18EEC95A207@.microsoft.com...
>
>|||Hi
I used scope instead of session. You can use the temporary tables in called
procedures but you can't create the temporary table in one sub-procedure and
then use it in the next it has to be created at the higher level.
If you posted and example that works when you use a global temporary table
and fails when local it may help.
John
"SalamElias" <eliassal@.online.nospam> wrote in message
news:B4CAD9DF-83B2-4111-AFDB-F96C27DDF7A2@.microsoft.com...
> Thansk for your response. As I said they are in the same SP.
> Can you please explain what do you mean by ' not in the same scope '.
> As I understand from what is indicated in BOL that nested SP in SP can
> call
> local temporary tables :
> --
> A local temporary table created in a stored procedure is dropped
> automatically when the stored procedure completes. The table can be
> referenced by any nested stored procedures executed by the stored
> procedure
> that created the table. The table cannot be referenced by the process
> which
> called the stored procedure that created the table.
> --
> My understanding is that second level sp can execute sql phrases against
> LTT
> in hosting SP, No?
> "John Bell" wrote:
>|||here we go
Here is 2 SPs, thje first one woth 2 '#' signe the second with one
I provided a create table which you can insert the following item
"3C17203-ME" in for test
purposes
For testing the SPs call them as follows (I have taken a lot of info and
joins from the original one)
bask_GetPriceForOneItem_VerSigNewsGroupe
s_2Signs 'C ', '018841ML', 'EUR',
'3C17203-ME', 'OI154315_61466'
bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign 'C ', '018841ML', 'EUR',
'3C17203-ME', 'OI154315_61466'
The table used by the simplified PROC
CREATE TABLE [dbo].[PRODUCT_NG] (
[PROD_MANF_SKU] [varchar] (20)
) ON [PRIMARY]
GO
SP with 2Signs
---
CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_2Signs
@.sCompanyNo varchar(2),
@.sCustomerNo varchar(8),
@.sCurrency char(3),
@.sSQL varchar(20),
@.sTableName varchar(255)
--@.WareHouse char(3)="11"
AS
SET NOCOUNT ON
Declare @.sFrom varchar(700)
Declare @.sWhereStart varchar(700)
Declare @.sFromWhereStart varchar(1900)
Declare @.lCount varchar(5)
Declare @.intCount int
Declare @.WEBGroup char(3)
Set @.WEBGroup = 'WEB'
Declare @.lstr_sTableName varchar(255)
declare @.strUniqueTable varchar(15)
Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as varchar(2) )
+ Cast(DATEPART (millisecond , getdate()) as varchar(3) )
Set @.lstr_sTableName = 'dbo.##CC' + Ltrim(Rtrim(@.sTableName)) +
Rtrim(@.sCustomerNo) + @.strUniqueTable
--Print @.lstr_sTableName
--Return
SET @.sSQL = REPLACE(@.sSQL,'"','''')
Set @.sFrom = " FROM PRODUCT_NG "
Set @.sWhereStart = " "
Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
Rtrim(@.sSQL)
EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
@.lstr_sTableName + " " + @.sFromWhereStart )
Exec ("Select oC_PricePub, fC_ManfSKU from " + @.lstr_sTableName)
EXECUTE ("DROP TABLE " + @.lstr_sTableName)
GO
---
The single sign proc
---
CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign
@.sCompanyNo varchar(2),
@.sCustomerNo varchar(8),
@.sCurrency char(3),
@.sSQL varchar(20),
@.sTableName varchar(255)
--@.WareHouse char(3)="11"
AS
SET NOCOUNT ON
Declare @.sFrom varchar(700)
Declare @.sWhereStart varchar(700)
Declare @.sFromWhereStart varchar(1900)
Declare @.lCount varchar(5)
Declare @.intCount int
Declare @.WEBGroup char(3)
Set @.WEBGroup = 'WEB'
Declare @.lstr_sTableName varchar(255)
declare @.strUniqueTable varchar(15)
Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as varchar(2) )
+ Cast(DATEPART (millisecond , getdate()) as varchar(3) )
Set @.lstr_sTableName = 'dbo.#CC' + Ltrim(Rtrim(@.sTableName)) +
Rtrim(@.sCustomerNo) + @.strUniqueTable
--Print @.lstr_sTableName
--Return
SET @.sSQL = REPLACE(@.sSQL,'"','''')
Set @.sFrom = " FROM PRODUCT_NG "
Set @.sWhereStart = " "
Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
Rtrim(@.sSQL)
EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
@.lstr_sTableName + " " + @.sFromWhereStart )
Exec ("Select oC_PricePub, fC_ManfSKU from " + @.lstr_sTableName)
EXECUTE ("DROP TABLE " + @.lstr_sTableName)
GO
---
"John Bell" wrote:

> Hi
> I used scope instead of session. You can use the temporary tables in calle
d
> procedures but you can't create the temporary table in one sub-procedure a
nd
> then use it in the next it has to be created at the higher level.
> If you posted and example that works when you use a global temporary table
> and fails when local it may help.
> John
> "SalamElias" <eliassal@.online.nospam> wrote in message
> news:B4CAD9DF-83B2-4111-AFDB-F96C27DDF7A2@.microsoft.com...
>
>|||Hi
This may wrap but try:
CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign
@.sCompanyNo varchar(2),
@.sCustomerNo varchar(8),
@.sCurrency char(3),
@.sSQL varchar(20),
@.sTableName varchar(255)
--@.WareHouse char(3)="11"
AS
SET NOCOUNT ON
Declare @.sFrom varchar(700)
Declare @.sWhereStart varchar(700)
Declare @.sFromWhereStart varchar(1900)
Declare @.lCount varchar(5)
Declare @.intCount int
Declare @.WEBGroup char(3)
Set @.WEBGroup = 'WEB'
Declare @.lstr_sTableName varchar(255)
declare @.strUniqueTable varchar(15)
Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as varchar(2) )
+ Cast(DATEPART (millisecond , getdate()) as varchar(3) )
Set @.lstr_sTableName = '#CC' + Ltrim(Rtrim(@.sTableName)) +
Rtrim(@.sCustomerNo) + @.strUniqueTable
--Print @.lstr_sTableName
--Return
SET @.sSQL = REPLACE(@.sSQL,'"','''')
Set @.sFrom = ' FROM dbo.PRODUCT_NG '
Set @.sWhereStart = ' '
Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
--Rtrim(@.sSQL)
EXEC ('BEGIN CREATE TABLE ' + @.lstr_sTableName + ' ( [fC_ManfSKU] [varchar]
(20), [oC_PricePub] int ) INSERT INTO ' + @.lstr_sTableName + ' (
[fC_ManfSKU], [oC_PricePub] ) SELECT PROD_MANF_SKU, 0 ' + @.sFromWhereStart
+ ' DROP TABLE ' + @.lstr_sTableName + ' END')
GO
The BEGIN and END show the scope for the temporary table.
John
"SalamElias" <eliassal@.online.nospam> wrote in message
news:BDC195B7-536E-4F47-A41E-BAE856520A3F@.microsoft.com...
> here we go
> Here is 2 SPs, thje first one woth 2 '#' signe the second with one
> I provided a create table which you can insert the following item
> "3C17203-ME" in for test
> purposes
> For testing the SPs call them as follows (I have taken a lot of info and
> joins from the original one)
> bask_GetPriceForOneItem_VerSigNewsGroupe
s_2Signs 'C ', '018841ML', 'EUR',
> '3C17203-ME', 'OI154315_61466'
> bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign 'C ', '018841ML', 'EUR',
> '3C17203-ME', 'OI154315_61466'
>
> The table used by the simplified PROC
> CREATE TABLE [dbo].[PRODUCT_NG] (
> [PROD_MANF_SKU] [varchar] (20)
> ) ON [PRIMARY]
> GO
>
> SP with 2Signs
> ---
> CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_2Signs
> @.sCompanyNo varchar(2),
> @.sCustomerNo varchar(8),
> @.sCurrency char(3),
> @.sSQL varchar(20),
> @.sTableName varchar(255)
> --@.WareHouse char(3)="11"
> AS
> SET NOCOUNT ON
> Declare @.sFrom varchar(700)
> Declare @.sWhereStart varchar(700)
> Declare @.sFromWhereStart varchar(1900)
> Declare @.lCount varchar(5)
> Declare @.intCount int
> Declare @.WEBGroup char(3)
> Set @.WEBGroup = 'WEB'
> Declare @.lstr_sTableName varchar(255)
> declare @.strUniqueTable varchar(15)
> Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as
> varchar(2) )
> + Cast(DATEPART (millisecond , getdate()) as varchar(3) )
> Set @.lstr_sTableName = 'dbo.##CC' + Ltrim(Rtrim(@.sTableName)) +
> Rtrim(@.sCustomerNo) + @.strUniqueTable
> --Print @.lstr_sTableName
> --Return
> SET @.sSQL = REPLACE(@.sSQL,'"','''')
> Set @.sFrom = " FROM PRODUCT_NG "
> Set @.sWhereStart = " "
> Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
> Rtrim(@.sSQL)
> EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
> @.lstr_sTableName + " " + @.sFromWhereStart )
> Exec ("Select oC_PricePub, fC_ManfSKU from " + @.lstr_sTableName)
> EXECUTE ("DROP TABLE " + @.lstr_sTableName)
> GO
> ---
> The single sign proc
> ---
> CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign
> @.sCompanyNo varchar(2),
> @.sCustomerNo varchar(8),
> @.sCurrency char(3),
> @.sSQL varchar(20),
> @.sTableName varchar(255)
> --@.WareHouse char(3)="11"
> AS
> SET NOCOUNT ON
> Declare @.sFrom varchar(700)
> Declare @.sWhereStart varchar(700)
> Declare @.sFromWhereStart varchar(1900)
> Declare @.lCount varchar(5)
> Declare @.intCount int
> Declare @.WEBGroup char(3)
> Set @.WEBGroup = 'WEB'
> Declare @.lstr_sTableName varchar(255)
> declare @.strUniqueTable varchar(15)
> Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as
> varchar(2) )
> + Cast(DATEPART (millisecond , getdate()) as varchar(3) )
> Set @.lstr_sTableName = 'dbo.#CC' + Ltrim(Rtrim(@.sTableName)) +
> Rtrim(@.sCustomerNo) + @.strUniqueTable
> --Print @.lstr_sTableName
> --Return
> SET @.sSQL = REPLACE(@.sSQL,'"','''')
> Set @.sFrom = " FROM PRODUCT_NG "
> Set @.sWhereStart = " "
> Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
> Rtrim(@.sSQL)
> EXECUTE ("SELECT PROD_MANF_SKU AS fC_ManfSKU, 0 as oC_PricePub into " +
> @.lstr_sTableName + " " + @.sFromWhereStart )
> Exec ("Select oC_PricePub, fC_ManfSKU from " + @.lstr_sTableName)
> EXECUTE ("DROP TABLE " + @.lstr_sTableName)
> GO
> ---
> "John Bell" wrote:
>|||Hi
To add...
There should be no need to worry about creating a unique name as this will
be done for you. This would then allow you to create the temporary table as
normal e.g.
CREATE TABLE #tmp1 ( id int not null identity, col1 varchar(10))
INSERT INTO #tmp1 ( col1 ) EXEC ('SELECT ''abc'' AS col1 UNION ALL SELECT
''def''')
SELECT * FROM #tmp1
DROP TABLE #tmp1
Your contention may be from using SELECT ... INTO as this may lock the
system tables in tempdb for a prolonged period. This was certainly an issue
on older versions of SQL Server and is a good practice to avoid.
John
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message
news:O5mwLfwsFHA.1328@.TK2MSFTNGP10.phx.gbl...
> Hi
> This may wrap but try:
> CREATE PROCEDURE bask_GetPriceForOneItem_VerSigNewsGroupe
s_1Sign
> @.sCompanyNo varchar(2),
> @.sCustomerNo varchar(8),
> @.sCurrency char(3),
> @.sSQL varchar(20),
> @.sTableName varchar(255)
> --@.WareHouse char(3)="11"
> AS
> SET NOCOUNT ON
> Declare @.sFrom varchar(700)
> Declare @.sWhereStart varchar(700)
> Declare @.sFromWhereStart varchar(1900)
> Declare @.lCount varchar(5)
> Declare @.intCount int
> Declare @.WEBGroup char(3)
> Set @.WEBGroup = 'WEB'
> Declare @.lstr_sTableName varchar(255)
> declare @.strUniqueTable varchar(15)
> Select @.strUniqueTable = Cast(DATEPART (second , getdate()) as
> varchar(2) )
> + Cast(DATEPART (millisecond , getdate()) as varchar(3) )
> Set @.lstr_sTableName = '#CC' + Ltrim(Rtrim(@.sTableName)) +
> Rtrim(@.sCustomerNo) + @.strUniqueTable
> --Print @.lstr_sTableName
> --Return
> SET @.sSQL = REPLACE(@.sSQL,'"','''')
> Set @.sFrom = ' FROM dbo.PRODUCT_NG '
> Set @.sWhereStart = ' '
> Set @.sFromWhereStart = Rtrim(Rtrim(@.sFrom) + Rtrim(@.sWhereStart) ) --+
> --Rtrim(@.sSQL)
> EXEC ('BEGIN CREATE TABLE ' + @.lstr_sTableName + ' ( [fC_ManfSKU]
> [varchar] (20), [oC_PricePub] int ) INSERT INTO ' + @.lstr_sTableName +
> ' ( [fC_ManfSKU], [oC_PricePub] ) SELECT PROD_MANF_SKU, 0 ' +
> @.sFromWhereStart + ' DROP TABLE ' + @.lstr_sTableName + ' END')
> GO
> The BEGIN and END show the scope for the temporary table.
> John
> "SalamElias" <eliassal@.online.nospam> wrote in message
> news:BDC195B7-536E-4F47-A41E-BAE856520A3F@.microsoft.com...
>|||So many thanks for your suggestion. As I said I have taken a lot of the T-SQ
L
off to make it simple.
The 'Select Into.....' phrase is followed by several dynamic sql as follows
EXEC ("Update ......bla bla " + mytemporaryTable + "blah bla ......"
So If I follow your first suggestion it means I shoyuld concatenate a ton of
sql phrases in one hus EXEC 'Begin ...END' which would be a pain for
debugging.
Of course the seond suggestion can not be used because it is straight T-SQL
and not dynamic
I would appreciate any other ideas or fixes if possible of course.
Salam
"John Bell" wrote:

> Hi
> To add...
> There should be no need to worry about creating a unique name as this will
> be done for you. This would then allow you to create the temporary table a
s
> normal e.g.
> CREATE TABLE #tmp1 ( id int not null identity, col1 varchar(10))
> INSERT INTO #tmp1 ( col1 ) EXEC ('SELECT ''abc'' AS col1 UNION ALL SELECT
> ''def''')
> SELECT * FROM #tmp1
> DROP TABLE #tmp1
> Your contention may be from using SELECT ... INTO as this may lock the
> system tables in tempdb for a prolonged period. This was certainly an issu
e
> on older versions of SQL Server and is a good practice to avoid.
> John
>
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message
> news:O5mwLfwsFHA.1328@.TK2MSFTNGP10.phx.gbl...
>
>|||Hi
The second suggestion is that the table is static but the SQL is dynamic, I
have never found a application that really required dynamic table
definitions. You may want to re-consider the design of the process and the
reasons you think it has to be dynamic.
You may also want to read http://www.sommarskog.se/dyn-search.html to see if
there is anything that can be used.
John
"SalamElias" <eliassal@.online.nospam> wrote in message
news:2D75B7F3-930F-4E6D-8255-56C05E898E97@.microsoft.com...
> So many thanks for your suggestion. As I said I have taken a lot of the
> T-SQL
> off to make it simple.
> The 'Select Into.....' phrase is followed by several dynamic sql as
> follows
> EXEC ("Update ......bla bla " + mytemporaryTable + "blah bla ......"
> So If I follow your first suggestion it means I shoyuld concatenate a ton
> of
> sql phrases in one hus EXEC 'Begin ...END' which would be a pain for
> debugging.
> Of course the seond suggestion can not be used because it is straight
> T-SQL
> and not dynamic
> I would appreciate any other ideas or fixes if possible of course.
> Salam
> "John Bell" wrote:
>