April 16, 2012

sp_attach_db (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_attach_db(nvarchar @dbname
, nvarchar @filename1
, nvarchar @filename2
, nvarchar @filename3
, nvarchar @filename4
, nvarchar @filename5
, nvarchar @filename6
, nvarchar @filename7
, nvarchar @filename8
, nvarchar @filename9
, nvarchar @filename10
, nvarchar @filename11
, nvarchar @filename12
, nvarchar @filename13
, nvarchar @filename14
, nvarchar @filename15
, nvarchar @filename16)

MetaData:

 create procedure sys.sp_attach_db  
@dbname sysname
, @filename1 nvarchar(260)
, @filename2 nvarchar(260) = NULL
, @filename3 nvarchar(260) = NULL
, @filename4 nvarchar(260) = NULL
, @filename5 nvarchar(260) = NULL
, @filename6 nvarchar(260) = NULL
, @filename7 nvarchar(260) = NULL
, @filename8 nvarchar(260) = NULL
, @filename9 nvarchar(260) = NULL
, @filename10 nvarchar(260) = NULL
, @filename11 nvarchar(260) = NULL
, @filename12 nvarchar(260) = NULL
, @filename13 nvarchar(260) = NULL
, @filename14 nvarchar(260) = NULL
, @filename15 nvarchar(260) = NULL
, @filename16 nvarchar(260) = NULL

as
declare @execstring nvarchar (max)
set nocount on

if ((@dbname is null or datalength(@dbname) = 0) or
(@filename1 is null or datalength(@filename1) = 0))
begin
raiserror (15004,-1,-1)
return (1)
end

-- build initial CREATE DATABASE
select @execstring = 'CREATE DATABASE '
+ quotename( @dbname , '[')
+ ' ON (FILENAME ='
+ ''''
+ REPLACE(@filename1,N'''',N'''''')
+ ''''

-- add any additional files

if (@filename2 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename2 ,N'''',N'''''')
+ ''''
end

if (@filename3 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename3 ,N'''',N'''''')
+ ''''
end

if (@filename4 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename4 ,N'''',N'''''')
+ ''''
end

if (@filename5 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename5 ,N'''',N'''''')
+ ''''
end

if (@filename6 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename6 ,N'''',N'''''')
+ ''''
end

if (@filename7 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename7 ,N'''',N'''''')
+ ''''
end

if (@filename8 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename8 ,N'''',N'''''')
+ ''''
end

if (@filename9 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename9 ,N'''',N'''''')
+ ''''
end

if (@filename10 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename10 ,N'''',N'''''')
+ ''''
end

if (@filename11 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename11 ,N'''',N'''''')
+ ''''
end

if (@filename12 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename12 ,N'''',N'''''')
+ ''''
end

if (@filename13 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename13 ,N'''',N'''''')
+ ''''
end

if (@filename14 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename14 ,N'''',N'''''')
+ ''''
end

if (@filename15 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename15 ,N'''',N'''''')
+ ''''
end

if (@filename16 is not null)
begin
select @execstring = @execstring
+ ' ), (FILENAME= '''
+ REPLACE(@filename16 ,N'''',N'''''')
+ ''''
end


-- note it as for attach
select @execstring = @execstring + ' ) FOR ATTACH'

EXEC (@execstring)

-- No need to raiserror as the CREATE DATABASE will do so
if @@error <> 0
return(1)

return (0) -- sp_attach_db

No comments:

Post a Comment

Total Pageviews