June 4, 2012

sp_MSuniquename (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_MSuniquename(nvarchar @seed
, int @start)

MetaData:

   
create procedure sys.sp_MSuniquename
@seed nvarchar(128), @start int = null
as
-- Return a unique name for sysobjects, based on a passed-in seed. --
set nocount on
declare @i int, @append nvarchar(10), @seedlen int, @temp nvarchar(128), @recalcseedlen int, @seedcharlen int
select @i = 1, @seedlen = datalength(@seed), @recalcseedlen = 1, @seedcharlen = 0
if (@start is not null and @start >= 0)
select @i = @start
while 1 < 2
begin
-- This is probably overkill, but start at max length of seed name, leaving room under OSQL_DBLSYSNAME_SET for @append. --
-- We'll work our way back along the string if more room needed (pathological user). --
select @append = ltrim(str(@i)) + N'__' + ltrim(str(@@spid))
if (@recalcseedlen = @i or @seedcharlen = 0)
begin
while @recalcseedlen <= @i
select @recalcseedlen = @recalcseedlen * 10
select @seedcharlen = @seedlen
if ((@seedlen + datalength(@append)) > 128) begin
select @seedlen = 128 - datalength(@append)

-- Get the charlen of this datalength for the substring() call. --
select @seedcharlen = @seedlen
-- exec sp_GetMBCSCharLen @seed, @seedlen, @seedcharlen out --
end -- Recalc seedlen --
end -- Check seedlen --

select @temp = substring(@seed, 1, @seedcharlen) + @append

-- If I don't set a limit somewhere, it's gonna look hung -- I'd rather get a nonunique error. --
if object_id(@temp) is null or @i > 999999 -- if increased, watch out for overflow of @recalcseedlen --
begin
set nocount off
select Name = @temp, Next = @i + 1
return 0
end
select @i = @i + 1
end

No comments:

Post a Comment

Total Pageviews