473,804 Members | 3,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global temp table permissions

I have a pivot table implementation, part of which is posted below. It
returns no errors in query analyzer, but when profiler is run, it shows
that "Error 208" is happening. I looked that up in BOL and it means
that an object doesn't exist. This block of code below works fine on my
local development machine, but not on our shared development server
until I go into the tempdb and make the user have the role db_owner.
Even wierder is that when I do a select * from ##pivot there is no
error, but if I specify the single column name (pivot) i.e. select
pivot from ##pivot, it takes the error...

Obviously this is a rights issue, but is there any way around this
other than making the user owner of tempdb??

declare @select varchar(8000), @PackageId int
set @PackageId = 10
set @select = '
select
Company = COALESCE(Users. Company, Contact.Company , ''''),
SubContractPack ageVendor.Id, SubContractPack ageVendor.isAct ive,
SubContractPack ageVendor.isAwa rded,
SubContractPack ageVendor.UserO rContactType,
SubContractPack ageVendor.UserO rContactId
FROM
SubContractPack ageVendor
LEFT JOIN SubContractPack age ON SubContractPack ageVendor.Packa geId =
SubContractPack age.Id

LEFT JOIN Users ON UserOrContactTy pe = ''User'' AND UserOrContactId =
Users.UserId
LEFT JOIN UserRoles ON UserOrContactTy pe = ''User'' AND
UserRoles.UserI d = Users.UserId AND UserRoles.Proje ctId =
SubContractPack age.ProjectId
LEFT JOIN Role ON Role.RoleId = UserRoles.RoleI d

LEFT JOIN Contact ON UserOrContactTy pe = ''Contact'' AND
UserOrContactId = Contact.Id

LEFT JOIN SubContractLine Item ON
SubContractLine Item.RefType = ''Package'' AND
SubContractLine Item.RefId = SubContractPack ageVendor.Packa geId
LEFT JOIN SubContractLine Item as SubContractPack ageVendorItem ON
SubContractPack ageVendorItem.R efType = ''PackageVendor '' AND
SubContractPack ageVendorItem.R efId = SubContractPack ageVendor.Id AND
SubContractPack ageVendorItem.R efSubId = SubContractLine Item.Id
Where
SubContractPack ageVendor.Packa geId = ' + CAST(@PackageId as varchar)
+ '
GROUP BY
SubContractPack ageVendor.Id, SubContractPack ageVendor.isAct ive,
SubContractPack ageVendor.isAwa rded, Users.Company, Contact.Company ,
SubContractPack ageVendor.UserO rContactType,
SubContractPack ageVendor.UserO rContactId'
--print @sql

declare @sumfunc varchar(100),
@pivot varchar(100),
@table varchar(100),
@FieldPrefix varchar(5),
@TotalFieldName varchar(50),
@PivotFieldFilt er varchar(1000)

select
@sumfunc ='Sum(isnull(Su bContractPackag eVendorItem.Tot al,0))' ,
@pivot ='SubContractLi neItem.Category ' ,
@table ='SubContractLi neItem' ,
@FieldPrefix='~ ' ,
@TotalFieldName = 'Total' ,
@PivotFieldFilt er = ' AND RefType=''Packa ge'' AND RefId=' +
CAST(@PackageId as varchar)

set nocount on

DECLARE @sql varchar(8000), @delim varchar(1), @TotalSql varchar(8000)
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + '
WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM ' +
@table + ' WHERE '
+ @pivot + ' Is Not Null ' + @PivotFieldFilt er)

SELECT @sql='', @sumfunc=stuff( @sumfunc, len(@sumfunc), 1, ' END)' )

SELECT @delim=CASE Sign( CharIndex('char ',
data_type)+Char Index('date', data_type) )
WHEN 0 THEN '' ELSE '''' END
FROM tempdb.informat ion_schema.colu mns
WHERE table_name='##p ivot' AND column_name='pi vot'

select * from ##pivot

DROP TABLE ##pivot

Jul 23 '05 #1
6 3762
pb648174 (go****@webpaul .net) writes:
I have a pivot table implementation, part of which is posted below. It
returns no errors in query analyzer, but when profiler is run, it shows
that "Error 208" is happening.
Because of deferred name resolution, you see quite a lot of 208 in
Profiler which are "false alarms". When SQL Server first compiles your
batch, ##pivot does not exist which causes a 208 error which is then
suppressed. When you hit a statement where ##pivot is referred, the
batch is recompiled.
I looked that up in BOL and it means that an object doesn't exist. This
block of code below works fine on my local development machine, but not
on our shared development server until I go into the tempdb and make the
user have the role db_owner. Even wierder is that when I do a select *
from ##pivot there is no error, but if I specify the single column name
(pivot) i.e. select pivot from ##pivot, it takes the error...


And that is a 208 and not a 207 ("Column does not exist"?) There could
be an old ##pivot which hanging around.

In any case, I'm not a friend of global temp tables, and I don't recommend
use of them. I don't know what your underlying problem is, but my favourite
is a spid-keyed table. That is, a permanent table, but which is intended
to hold any data only transitionary. The first column is spid, and in
the simplest form you use @@spid for the value.

Now, @@spid, does not work well, if you are to share the table between
processes. For this reason, we use negative values in these cases. You
could simply do:

BEGIN TRANSACTION
SELECT @nextksy = coalesce(MIN(sp id), 0) - 1 FROM tbl (UPDLOCK)

INSERT tbl (spid, ...) VALUES (@nextkey, ...)

COMMIT TRANSACTION

Of course you should take measures to have old data removed from the
table once you are done with it. But that is really not any different
from the global temp table. Except, that is, you have more control
over it.

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #2
Why does giving the user running the process db_owner access solve the
problem? Is there some other way other than rewriting large blocks of
working code?

Jul 23 '05 #3
pb648174 (go****@webpaul .net) writes:
Why does giving the user running the process db_owner access solve the
problem? Is there some other way other than rewriting large blocks of
working code?


Judging from your post, the code is not working in the general case, only
in a protected environment.

I don't know where db_owner comes into play. I played around a little
with a plain user and global temp tables, but I did not ran into any
problems.

Then again you have not specified what "not works on the shared development
server" means. Do you get error messages? Incorrect results?

Maybe the problem is access rights to the other tables, and not with
the global temp table?

Anyway, global temp tables, is in my opinion a poor solution, because
there is always a lot uigly what-if scenarios. Whence my recommentation
to change to something better.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
Actually I specifically said "It returns no errors in query analyzer,
but when profiler is run, it shows that "Error 208" is happening" - it
is only on one machine that it does this, and the fix for that machine
was to give the user running the command db_owner access to the tempdb
database.

The reason I am using the global temp table implementation for this is
because the below statement correctly (and dynamically) creates the
type of the column automagically. If there is a way to do that with the
method you suggested, or better yet, to find out why giving the tempdb
db_owner access fixes the 208 error and keep the existing working,
tested and released code, that would be great. Lastly, if there is a
better way to perform a pivot table in SQL Server(with working sample),
I am all ears - this was simply the best I found that actually works.

EXEC ('SELECT ' + @pivot + ' AS pivot INTO ##pivot FROM ' + @table + '
WHERE 1=2')
EXEC ('INSERT INTO ##pivot SELECT DISTINCT ' + @pivot + ' FROM
' +
@table + ' WHERE '
+ @pivot + ' Is Not Null ' + @PivotFieldFilt er)

Jul 23 '05 #5
pb648174 (go****@webpaul .net) writes:
Actually I specifically said "It returns no errors in query analyzer,
but when profiler is run, it shows that "Error 208" is happening" - it
And as I explained to you, the 208 in Profiler is a false alarm.

So you don't get any error, but how then you do conclude that it doesn't
work? Or don't you really have any problem at all? Do you get incorrect
results, or what is your real problem?
is only on one machine that it does this, and the fix for that machine
was to give the user running the command db_owner access to the tempdb
database.
Or because when you reran the batch, the plan was in the cache, and the
208 did not happen behind the scenes.

db_owner could possibly have something to do with your reading
of INFORMATION_SCH EMA.COLUMNS, since that view is supposed to only
show things you have access to, so somesuch. Try using syscolumns
instead. (But here I am guessing very wildly of what you problem you
really have.)
The reason I am using the global temp table implementation for this is
because the below statement correctly (and dynamically) creates the
type of the column automagically. If there is a way to do that with the
method you suggested, or better yet, to find out why giving the tempdb
db_owner access fixes the 208 error and keep the existing working,
Again, the 208 you see in Profiler is not a problem!

Rather than using SELECT INTO, create the table with CREATE TABLE. Then
you can use a plain #temp table.
tested and released code, that would be great. Lastly, if there is a
better way to perform a pivot table in SQL Server(with working sample),
I am all ears - this was simply the best I found that actually works.


Popular for crosstabs and that sort of thing is RAC,
see http://www.rac4sql.net.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #6
Ok, that worked great, thanks.

I was thinking I couldn't used the #temp table since it was doing an
exec sql select and had already tried the @temp table style... I also
wish I would have seen that reference to tempdb in there, that was a
good educated guess. I pulled that pivot code from another site,
apparently it had some problems. It is now working well with a normal
#temp table (although I have to do some shenanigans to get the type of
the column correct, which that statement with the global temp table did
so easily.)

Thanks for the help!

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13744
by: Corrine | last post by:
Hi, I am creating a global temporary table that is session-specific. I insert a BLOB into this table, and then select the BLOB from this table into a ResultSet. The ResultSet sees this BLOB object, and I am able to get the binary input stream from this blob. However, when I invoke InputStream.read(byte) on this input stream, I get the following exception: java.io.IOException: ORA-01410: invalid ROWID
10
10824
by: Ranga | last post by:
I was unable to run the statement "CREATE GLOBAL TEMPORARY TABLE" on unix version of DB2, it gave the follwing error db2 => create global temporary table temp ( OGI_SYS_NR char(8) ) DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL0104N An unexpected token "create global temporary table temp ( OGI_S" was
0
2602
by: gwaddell | last post by:
I have a stored procedure that is loading data into a global temp table. Here is all the code for that stored procedure. CREATE PROCEDURE AS SET NOCOUNT ON BEGIN
2
4141
by: chettiar | last post by:
I am creating a procedure A which is creating a global temporary table DECLARE GLOBAL TEMPORARY TABLE session.temp (Service CHAR(2), CustomerServiceTypeId INTEGER) WITH REPLACE ON COMMIT PRESERVE ROWS; I am able to compile the proceudre. But when I try to compile procedure B which is referencing the temporary table in procedure A, I get the error.
16
10331
by: pukivruki | last post by:
hi, I wish to create a temporary table who's name is dynamic based on the argument. ALTER PROCEDURE . @PID1 VARCHAR(50), @PID2 VARCHAR(50), @TICKET VARCHAR(20)
1
4643
by: crazy_jutt | last post by:
hi, anyone knows if i can create index on global temp tables if yes, will i use session schema for indexes also ? if yes, can i create all kinds like unique, clustered, mdc etc indexes in global temp table ? please direct me to some web resource where i can find what all i can do and i cannot do with global temp tables
5
8844
by: Rahul B | last post by:
Hi, I have very little knowledge about creating Procedures/functions in DB2. When i tried to create the test function like CREATE FUNCTION GET_TEST (P_TEST_ID INTEGER, P_SEL_OR_SORT INTEGER,
3
2454
by: Lennart | last post by:
Any thoughts on the following scenario anyone? During a performance test I discovered that the application asked one specific query extremely often. It turned out that this particular query where asked 25/50/100 or 200 times from a "htmlpage", dependent of user preferences. I figured that using a global temp table, looping and inserting, then join would do the trick. However, it turned out that this killed performance totally. Why,...
1
1493
by: romV | last post by:
Hi, I have a procedure that creates the global temp table (##temp) inside a procedure which is populated with data when it is run for the first time. And when the procedure is executed second time, it uses that temp table instead of loading it all over. The trouble I am having is that when I execute the procedure from Asp.NET the temp table stays there as long as i call the same procedure, but as soon as I call another procedure the temp...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.