Unix Technical Forum

SPROC won't execute - insufficient permissions

This is a discussion on SPROC won't execute - insufficient permissions within the SQL Server forums, part of the Microsoft SQL Server category; --> I'm trying to use the SPROC below (courtesy of Erland!) to capture the error message but it fails owing ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 07:49 PM
teddysnips@hotmail.com
 
Posts: n/a
Default SPROC won't execute - insufficient permissions

I'm trying to use the SPROC below (courtesy of Erland!) to capture the
error message but it fails owing to insufficient permissions (I can't
reproduce it just now, but I think it's because it can't get access to
the DBCC OUTPUTBUFFER).

How do I give the SPROC permission to execute?

Many thanks

Edward


CREATE PROCEDURE stpShowErrorMessage @errmsg nvarchar(500) OUTPUT AS
DECLARE @dbccrow nchar(77),
@msglen int,
@lenstr nchar(2),
@sql nvarchar(2000),
@s tinyint

-- Catch the output buffer.
CREATE TABLE #DBCCOUT (col1 nchar(77) NOT NULL)
INSERT INTO #DBCCOUT
EXEC ('DBCC OUTPUTBUFFER(@@spid)')

-- Set up a cursor over the table. We skip the first
-- row, because there is nothing of interest.
DECLARE error_cursor CURSOR STATIC FORWARD_ONLY FOR
SELECT col1
FROM #DBCCOUT
WHERE left(col1, 8) <> replicate('0', 8)
ORDER BY col1

-- Init variable, and open cursor.
SELECT @errmsg = ''
OPEN error_cursor
FETCH NEXT FROM error_cursor INTO @dbccrow

-- On this first row we find the length.
SELECT @lenstr = substring(@dbccrow, 15, 2)

-- Convert hexstring to int
SELECT @sql = 'SELECT @int = convert(int, 0x00' + @lenstr + ')'
EXEC sp_executesql @sql, N'@int int OUTPUT', @msglen OUTPUT

-- @s is where the text part of the buffer starts.
SELECT @s = 62

-- Now assemble rest of string.
WHILE @@FETCH_STATUS = 0 AND datalength(@errmsg) - 1 < 2 * @msglen
BEGIN
SELECT @errmsg = @errmsg + substring(@dbccrow, @s + 1, 1) +
substring(@dbccrow, @s + 3, 1) +
substring(@dbccrow, @s + 5, 1) +
substring(@dbccrow, @s + 7, 1) +
substring(@dbccrow, @s + 9, 1) +
substring(@dbccrow, @s + 11, 1) +
substring(@dbccrow, @s + 13, 1) +
substring(@dbccrow, @s + 15, 1)
FETCH NEXT FROM error_cursor INTO @dbccrow
END

CLOSE error_cursor
DEALLOCATE error_cursor

-- Now chop first character which is the length, and cut after end.
SELECT @errmsg = substring(@errmsg, 2, @msglen)
GO

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 07:49 PM
Dan Guzman
 
Posts: n/a
Default Re: SPROC won't execute - insufficient permissions

> How do I give the SPROC permission to execute?

Since DBCC OUTPUTBUFFER can only be executed by sysadmin role members, you
must be a sysadmin to use this technique to get the error message. I
believe you will find this disclaimer in Erland's article.

SQL 2005 has structured error handling that allows you can get error details
in a CATCH block without the kludge.

--
Hope this helps.

Dan Guzman
SQL Server MVP

<teddysnips@hotmail.com> wrote in message
news:1151658530.478948.38290@x69g2000cwx.googlegro ups.com...
> I'm trying to use the SPROC below (courtesy of Erland!) to capture the
> error message but it fails owing to insufficient permissions (I can't
> reproduce it just now, but I think it's because it can't get access to
> the DBCC OUTPUTBUFFER).
>
> How do I give the SPROC permission to execute?
>
> Many thanks
>
> Edward
>
>
> CREATE PROCEDURE stpShowErrorMessage @errmsg nvarchar(500) OUTPUT AS
> DECLARE @dbccrow nchar(77),
> @msglen int,
> @lenstr nchar(2),
> @sql nvarchar(2000),
> @s tinyint
>
> -- Catch the output buffer.
> CREATE TABLE #DBCCOUT (col1 nchar(77) NOT NULL)
> INSERT INTO #DBCCOUT
> EXEC ('DBCC OUTPUTBUFFER(@@spid)')
>
> -- Set up a cursor over the table. We skip the first
> -- row, because there is nothing of interest.
> DECLARE error_cursor CURSOR STATIC FORWARD_ONLY FOR
> SELECT col1
> FROM #DBCCOUT
> WHERE left(col1, 8) <> replicate('0', 8)
> ORDER BY col1
>
> -- Init variable, and open cursor.
> SELECT @errmsg = ''
> OPEN error_cursor
> FETCH NEXT FROM error_cursor INTO @dbccrow
>
> -- On this first row we find the length.
> SELECT @lenstr = substring(@dbccrow, 15, 2)
>
> -- Convert hexstring to int
> SELECT @sql = 'SELECT @int = convert(int, 0x00' + @lenstr + ')'
> EXEC sp_executesql @sql, N'@int int OUTPUT', @msglen OUTPUT
>
> -- @s is where the text part of the buffer starts.
> SELECT @s = 62
>
> -- Now assemble rest of string.
> WHILE @@FETCH_STATUS = 0 AND datalength(@errmsg) - 1 < 2 * @msglen
> BEGIN
> SELECT @errmsg = @errmsg + substring(@dbccrow, @s + 1, 1) +
> substring(@dbccrow, @s + 3, 1) +
> substring(@dbccrow, @s + 5, 1) +
> substring(@dbccrow, @s + 7, 1) +
> substring(@dbccrow, @s + 9, 1) +
> substring(@dbccrow, @s + 11, 1) +
> substring(@dbccrow, @s + 13, 1) +
> substring(@dbccrow, @s + 15, 1)
> FETCH NEXT FROM error_cursor INTO @dbccrow
> END
>
> CLOSE error_cursor
> DEALLOCATE error_cursor
>
> -- Now chop first character which is the length, and cut after end.
> SELECT @errmsg = substring(@errmsg, 2, @msglen)
> GO
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 07:49 PM
teddysnips@hotmail.com
 
Posts: n/a
Default Re: SPROC won't execute - insufficient permissions


Dan Guzman wrote:

> > How do I give the SPROC permission to execute?

>
> Since DBCC OUTPUTBUFFER can only be executed by sysadmin role members, you
> must be a sysadmin to use this technique to get the error message. I
> believe you will find this disclaimer in Erland's article.
>
> SQL 2005 has structured error handling that allows you can get error details
> in a CATCH block without the kludge.


The SPROC executes in a Job, which is owned by SA, which is a member of
the sysadmin role. Where am I going wrong?!?

Unfortunately, SQL2005 is not an option.

Edward

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 07:49 PM
Erland Sommarskog
 
Posts: n/a
Default Re: SPROC won't execute - insufficient permissions

(teddysnips@hotmail.com) writes:
> The SPROC executes in a Job, which is owned by SA, which is a member of
> the sysadmin role. Where am I going wrong?!?


Probably in the assumption in the ownership of the job.

Could help if you posted the complete error message.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:30 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com