{"id":943,"date":"2021-02-16T14:13:09","date_gmt":"2021-02-16T17:13:09","guid":{"rendered":"http:\/\/jonescarvalho.com\/Blog\/?p=943"},"modified":"2021-02-16T14:13:09","modified_gmt":"2021-02-16T17:13:09","slug":"restore-database-com-cursor","status":"publish","type":"post","link":"https:\/\/jonescarvalho.com\/Blog\/?p=943","title":{"rendered":"Restore Database com Cursor"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>DECLARE @FilesCmdshell TABLE (\n    outputCmd NVARCHAR (255)\n)   \nDECLARE @FilesCmdshellCursor CURSOR \nDECLARE @FilesCmdshellOutputCmd AS NVARCHAR(255)\n\nINSERT INTO @FilesCmdshell (outputCmd) EXEC master.sys.xp_cmdshell 'dir \/B  C:\\Backup\\*.bak'    \nSET @FilesCmdshellCursor = CURSOR FOR SELECT outputCmd FROM @FilesCmdshell\n\nOPEN @FilesCmdshellCursor\nFETCH NEXT FROM @FilesCmdshellCursor INTO @FilesCmdshellOutputCmd\nWHILE @@FETCH_STATUS = 0\nBEGIN   \n    DECLARE @sqlRestore NVARCHAR(MAX) = 'RESTORE DATABASE &#91;' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + '] FROM  DISK = N''C:\\Backup\\' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + '.bak'' WITH  FILE = 1,  MOVE N''' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + ''' TO N''C:\\Microsoft SQL Server\\SQLINSTANCE\\MSSQL\\DATA\\' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + '.mdf'',  MOVE N''' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + '_log'' TO N''C:\\Microsoft SQL Server\\SQLINSTANCE\\MSSQL\\DATA\\' + SUBSTRING(@FilesCmdshellOutputCmd, 0, CHARINDEX('.', @FilesCmdshellOutputCmd)) + '_log.ldf'', NOUNLOAD,  STATS = 10'\n    EXEC(@sqlRestore)\n\n    FETCH NEXT FROM @FilesCmdshellCursor INTO @FilesCmdshellOutputCmd\nEND<\/code><\/pre>\n\n\n\n<p>para configurar o xp_cmdshell<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- To allow advanced options to be changed.  \nEXEC sp_configure 'show advanced options', 1;  \nGO  \n-- To update the currently configured value for advanced options.  \nRECONFIGURE;  \nGO  \n-- To enable the feature.  \nEXEC sp_configure 'xp_cmdshell', 1;  \nGO  \n-- To update the currently configured value for this feature.  \nRECONFIGURE;  \nGO  <\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>para configurar o xp_cmdshell<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-943","post","type-post","status-publish","format-standard","hentry","category-dicas"],"_links":{"self":[{"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/943"}],"collection":[{"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=943"}],"version-history":[{"count":1,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions"}],"predecessor-version":[{"id":944,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions\/944"}],"wp:attachment":[{"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jonescarvalho.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}