<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DkEMQH0-eCp7ImA9WhRaE0o.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915</id><updated>2012-02-16T00:44:41.350-06:00</updated><title>Adam J. Weigold</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://www.adamweigold.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://www.adamweigold.com/" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>5</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/AdamJWeigold" /><feedburner:info uri="adamjweigold" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;C0cCQ3Y-cSp7ImA9WhRVEkw.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915.post-1963364117182401284</id><published>2012-01-10T09:52:00.000-06:00</published><updated>2012-01-10T09:57:42.859-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-10T09:57:42.859-06:00</app:edited><title>Using a MultpartRequestResolver with Spring and using Spring Security concurrently</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/KvHA3gnU3zOQcxUl-6fn3CpEo6c/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KvHA3gnU3zOQcxUl-6fn3CpEo6c/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/KvHA3gnU3zOQcxUl-6fn3CpEo6c/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/KvHA3gnU3zOQcxUl-6fn3CpEo6c/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;When using Spring Security, the CommonsMultipartResolver will not work.  Why?  Because the MultipartHttpServletRequest will be wrapped in a SecurityContextHolderAwareRequestWrapper, and will not be matched.&lt;/p&gt;

&lt;p&gt;Of course, we don't want to fall back to just taking an HttpServletRequest as a parameter in our RequestMapping and parsing it out, we need to work smarter than that!&lt;/p&gt;

&lt;p&gt;The best solution I could come up with is registering a custom WebArgumentResolver (below).  But any readers out there have a better solution, please share!&lt;/p&gt;

&lt;p&gt;Resolver:
&lt;pre class="brush:java"&gt;
public class SecurityContextWrappedMultipartRequestArgumentResolver implements WebArgumentResolver {

    private final CommonsMultipartResolver commonsMultipartResolver;

    public SecurityContextWrappedMultipartRequestArgumentResolver(){
        this.commonsMultipartResolver = new CommonsMultipartResolver();
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
        if (MultipartHttpServletRequest.class.equals(methodParameter.getParameterType())) {
            Object object = webRequest.getNativeRequest();
            if (! (object instanceof SecurityContextHolderAwareRequestWrapper)) {
                return UNRESOLVED;
            }
            HttpServletRequest request = (HttpServletRequest) object;
            if (!ServletFileUpload.isMultipartContent(request)) {
                return UNRESOLVED;
            }
            SecurityContextHolderAwareRequestWrapper requestWrapper = (SecurityContextHolderAwareRequestWrapper) request;
            return commonsMultipartResolver.resolveMultipart(requestWrapper);
        }
        return UNRESOLVED;
    }
}

&lt;/pre&gt;
&lt;/p&gt;
&lt;p&gt;Then we just wire it up into our HandlerAdapter (your config may vary):

&lt;pre class="brush:java"&gt;
    @Bean
    public HandlerAdapter handlerAdapter() {
        final AnnotationMethodHandlerAdapter handlerAdapter = new AnnotationMethodHandlerAdapter();
        handlerAdapter.setCustomArgumentResolver(new SecurityContextWrappedMultipartRequestArgumentResolver());
        handlerAdapter.setAlwaysUseFullPath(true);
        List&amp;lt;HttpMessageConverter&amp;lt;?&amp;gt;&amp;gt; converterList = new ArrayList&amp;lt;HttpMessageConverter&amp;lt;?&amp;gt;&amp;gt;();
        converterList.addAll(Arrays.asList(handlerAdapter.getMessageConverters()));
        converterList.add(jibxHttpMessageConverter);
        converterList.add(gsonHttpMessageConverter);
        converterList.add(csvLocalizationConverter);
        converterList.add(protobufLocalizationConverter);
        handlerAdapter.setMessageConverters(converterList.toArray(new HttpMessageConverter&lt;?&gt;[converterList.size()]));
        return handlerAdapter;
    }
&lt;/pre&gt;
&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8526986393854881915-1963364117182401284?l=www.adamweigold.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdamJWeigold/~4/lw7mg2-OxvY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.adamweigold.com/feeds/1963364117182401284/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.adamweigold.com/2012/01/using-multpartrequestresolvers-with.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/1963364117182401284?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/1963364117182401284?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AdamJWeigold/~3/lw7mg2-OxvY/using-multpartrequestresolvers-with.html" title="Using a MultpartRequestResolver with Spring and using Spring Security concurrently" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.adamweigold.com/2012/01/using-multpartrequestresolvers-with.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkYFQH8yeip7ImA9WhRREE0.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915.post-5336546288791646230</id><published>2011-11-22T14:43:00.001-06:00</published><updated>2011-11-22T16:15:11.192-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-22T16:15:11.192-06:00</app:edited><title>Hibernate, ElementCollection, and Transactions</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/usjiIxHe0f7iKxOzem93sEuGd8I/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/usjiIxHe0f7iKxOzem93sEuGd8I/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/usjiIxHe0f7iKxOzem93sEuGd8I/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/usjiIxHe0f7iKxOzem93sEuGd8I/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;Hibernate implemented @ElementCollection in the JPA by binding the persistence of the ElementCollection of a new entity at the end of the transaction, and NOT at the time you tell the EntityManager to persist.&amp;nbsp; Under most use cases, this should not be a problem, however it does mean that you cannot detach the entity from the EntityManager prior to ending the Transaction.&lt;br /&gt;
&lt;br /&gt;
For example, the following will not persist your ElementCollection.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java"&gt;
@Entity
@Table(name = "ParentEntity")
public class ParentEntity {

    @Id
    @GeneratedValue
    @Column(name= "ParentId")
    private long parentId;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "ChildrenNames", joinColumns = @JoinColumn(name = "ParentId"))
    @Column(name = "ChildName")
    private Set&amp;lt;String&amp;gt; children;

    public Set&amp;lt;String&amp;gt; getChildren() {
        return children;
    }

    public void setChildren(Set&amp;lt;String&amp;gt; children) {
        this.children = children;
    }

    @Column(name = "ParentName")
    private String parentName;

    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }
}
&lt;/pre&gt;

&lt;pre class="brush:java"&gt;
@Service
public class ParentServiceImpl implements ParentService {

    @Autowired
    ParentRepository parentRepository;

    @Transactional
    public void saveParent(ParentEntity parentEntity){
        parentRepository.saveParent(parentEntity);
        parentRepository.detach(parentEntity);
    }
}
&lt;/pre&gt;

&lt;pre class="brush:java"&gt;
@Repository
public class ParentRepository {

    @PersistenceContext

    EntityManager entityManager;

    public void saveParent(ParentEntity parentEntity){
        entityManager.persist(parentEntity);
    }

    public void detachParent(ParentEntity parentEntity){
        entityManager.detach(parentEntity);
    }

    public ParentEntity getParentByName(String parentName){
        TypedQuery&amp;lt;ParentEntity&amp;gt; query = entityManager.createQuery("SELECT p FROM ParentEntity p WHERE p.parentName = :parentName", ParentEntity.class);
        query.setParameter("parentName", parentName);
        return query.getSingleResult();
    }
}
&lt;/pre&gt;
&lt;br /&gt;
You will likely not run into situations like this, however I'm posting as I ran into some code that I was refactoring for Spring 3.1.&amp;nbsp; 3.1 did not like nested @Transactions on a particular thread, and in the code's original design, it was detaching the entity on the nested item to avoid conflicts, I removed the nested @Transaction but did not notice the detach, and spent days figuring out why hibernate was not persisting the collection.&amp;nbsp; The answer is, as stated above, hibernate does not persist the collection at .persist, but on commit.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8526986393854881915-5336546288791646230?l=www.adamweigold.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdamJWeigold/~4/XFN4INigu4E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.adamweigold.com/feeds/5336546288791646230/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.adamweigold.com/2011/11/hibernate-elementcollection-and.html#comment-form" title="1 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/5336546288791646230?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/5336546288791646230?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AdamJWeigold/~3/XFN4INigu4E/hibernate-elementcollection-and.html" title="Hibernate, ElementCollection, and Transactions" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><thr:total>1</thr:total><feedburner:origLink>http://www.adamweigold.com/2011/11/hibernate-elementcollection-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEYAQn8zeip7ImA9WhRSGUU.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915.post-514310096868722386</id><published>2011-11-22T11:14:00.001-06:00</published><updated>2011-11-22T11:15:43.182-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-22T11:15:43.182-06:00</app:edited><title>CIFS share accessed in Linux returning 'cannot allocate memory'?</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/HiZvX5INsFrya1UEnaum8N7P8XY/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HiZvX5INsFrya1UEnaum8N7P8XY/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/HiZvX5INsFrya1UEnaum8N7P8XY/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/HiZvX5INsFrya1UEnaum8N7P8XY/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;a href="http://www.codenition.com/solving-cannot-allocate-memory-error-on-windows-7-linux-cifs-mounts"&gt;http://www.codenition.com/solving-cannot-allocate-memory-error-on-windows-7-linux-cifs-mounts&lt;/a&gt;&lt;br /&gt;
From the blog post:&lt;br /&gt;
Set the following registry key to ’&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;1&lt;/span&gt;′:&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\LargeSystemCache&lt;/div&gt;
and set the following registry key to ’&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;3&lt;/span&gt;′:&lt;br /&gt;
&lt;div style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;
HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\Size&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8526986393854881915-514310096868722386?l=www.adamweigold.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdamJWeigold/~4/ucQDUxFem3A" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.adamweigold.com/feeds/514310096868722386/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.adamweigold.com/2011/11/cifs-share-accessed-in-linux-returning.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/514310096868722386?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/514310096868722386?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AdamJWeigold/~3/ucQDUxFem3A/cifs-share-accessed-in-linux-returning.html" title="CIFS share accessed in Linux returning 'cannot allocate memory'?" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.adamweigold.com/2011/11/cifs-share-accessed-in-linux-returning.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEcERnY9fCp7ImA9WhRSGUU.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915.post-5621798299987329345</id><published>2011-11-22T11:12:00.001-06:00</published><updated>2011-11-22T11:13:27.864-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-22T11:13:27.864-06:00</app:edited><title>Exporting all tables to CSV in SQL Server</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/Xu5JpvYY805ni4LigRe3ykkxQNE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Xu5JpvYY805ni4LigRe3ykkxQNE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/Xu5JpvYY805ni4LigRe3ykkxQNE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/Xu5JpvYY805ni4LigRe3ykkxQNE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;The following script will output a list of statements to use bcp to export all tables to CSV. To use another format, see the bcp documentation.

&lt;br /&gt;
&lt;pre class="brush:sql"&gt;USE vidicom
SELECT 'exec master..xp_cmdshell'
+ ' '''
+ 'bcp'
+ ' ' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME
+ ' out'
+ ' E:\releasedDB\VV_6.1\prod\'
+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '.csv'
+ ' -c'
+ ' -t,'
+ ' -T'
+ ' -S' + @@SERVERNAME
+ ''''
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8526986393854881915-5621798299987329345?l=www.adamweigold.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdamJWeigold/~4/HdrVGp9Rdaw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.adamweigold.com/feeds/5621798299987329345/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.adamweigold.com/2011/11/exporting-all-tables-to-csv-in-sql.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/5621798299987329345?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/5621798299987329345?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AdamJWeigold/~3/HdrVGp9Rdaw/exporting-all-tables-to-csv-in-sql.html" title="Exporting all tables to CSV in SQL Server" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.adamweigold.com/2011/11/exporting-all-tables-to-csv-in-sql.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C08CR3c5fip7ImA9WhRSGUU.&quot;"><id>tag:blogger.com,1999:blog-8526986393854881915.post-7287048648497473716</id><published>2011-11-22T11:09:00.001-06:00</published><updated>2011-11-22T11:11:06.926-06:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-22T11:11:06.926-06:00</app:edited><title>Deleting a database remotely with SQL Server</title><content type="html">
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/fEm3FiYfyIlHhHawapS8aFk6UFw/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fEm3FiYfyIlHhHawapS8aFk6UFw/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/fEm3FiYfyIlHhHawapS8aFk6UFw/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/fEm3FiYfyIlHhHawapS8aFk6UFw/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;This should never be done on a production server, as it will open up 
security risks.  This is useful for integration tests and utilities.

Replace %dbName% appropriately

&lt;br /&gt;
&lt;pre class="brush:sql"&gt;BEGIN
 IF EXISTS (SELECT * FROM tempdb.sys.tables WHERE name LIKE '#dbFiles%')
  DROP TABLE #dbFiles
END

EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE

DECLARE @dbFile VARCHAR(8000)
DECLARE @cmd VARCHAR(8000)
SELECT physical_name INTO #dbFiles FROM %dbName%.sys.database_files

ALTER DATABASE %dbName% SET OFFLINE WITH ROLLBACK IMMEDIATE
DROP DATABASE %dbName%

SELECT * FROM #dbFiles

DECLARE cur CURSOR LOCAL FOR
 SELECT physical_name FROM #dbFiles
 
OPEN cur

FETCH next FROM cur INTO @dbFile

WHILE @@FETCH_STATUS = 0 BEGIN
 SET @cmd = 'del "' + @dbFile + '"'
 EXEC master.dbo.xp_cmdshell @cmd
 FETCH next FROM cur INTO @dbFile
END
CLOSE cur
DEALLOCATE cur
DROP TABLE #dbFiles
&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8526986393854881915-7287048648497473716?l=www.adamweigold.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/AdamJWeigold/~4/1Qmi4X-mlcI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://www.adamweigold.com/feeds/7287048648497473716/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.adamweigold.com/2011/11/deleting-database-remotely-with-sql.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/7287048648497473716?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8526986393854881915/posts/default/7287048648497473716?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/AdamJWeigold/~3/1Qmi4X-mlcI/deleting-database-remotely-with-sql.html" title="Deleting a database remotely with SQL Server" /><author><name>Adam Weigold</name><uri>https://profiles.google.com/116182780074037826670</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-GXHg21aR5jY/AAAAAAAAAAI/AAAAAAAAAB0/PGKvqanLBKU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://www.adamweigold.com/2011/11/deleting-database-remotely-with-sql.html</feedburner:origLink></entry></feed>

