Delete all objects from a database in SQL Server

0

Posted by fred | Posted in Database, Microsoft | Posted on 06-11-2009

Tags: , , , , ,

use myDatabase
declare @n char(1)
set @n = char(10)
declare @stmt nvarchar(max)
– procedures
select @stmt = isnull( @stmt + @n, ” ) +
‘drop procedure [' + name + ']‘
from sys.procedures
– synonyms
select @stmt = isnull( @stmt + @n, ” ) +
‘drop synonym [' + name + ']‘
from sys.synonyms
– check constraints
select @stmt = isnull( @stmt + @n, ” ) +
‘alter table [' + object_name( parent_object_id ) + '] drop constraint [' + name + ']‘
from sys.check_constraints
– functions
select @stmt = isnull( @stmt + @n, ” ) +
‘drop function [' + name + ']‘
from sys.objects
where type in ( ‘FN’, ‘IF’, ‘TF’ )
– views
select @stmt = isnull( @stmt + @n, ” ) +
‘drop view [' + name + ']‘
from sys.views
– foreign keys
select @stmt = isnull( @stmt + @n, ” ) +
‘alter table [' + object_name( parent_object_id ) + '] drop constraint [' + name + ']‘
from sys.foreign_keys
– tables
select @stmt = isnull( @stmt + @n, ” ) +
‘drop table [' + name + ']‘
from sys.tables
– user defined types
select @stmt = isnull( @stmt + @n, ” ) +
‘drop type [' + name + ']‘
from sys.types
where is_user_defined = 1
exec sp_executesql @stmt
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Create XML easily from SQL Server 2005

0

Posted by fred | Posted in Database, Microsoft | Posted on 16-07-2009

Tags: , , , ,

I found this very elegant way to generate XML directly from an SQL Server query:

SELECT

lastName AS [LastName],

firstName AS [FirstName],

adressLine AS [Address/Line],

adressZip AS [Address/Zip],

adressCity AS [Address/city],

emailAddress AS [ContactDetails/emailaddress],

website1 AS [ContactDetails/websites/website/website1],

website2 AS [ContactDetails/websites/website/website2],

telephone AS [ContactDetails/telephone]

FROM employees

FOR XML PATH (‘Employees’) , ROOT (‘Employee’), ELEMENTS

This query will generate you the (nested) tags including the headers of the file.

 

The only limitation that I found with this method is the length of the identifier. If the number of characters of the identifier is over 128, SQL server will reject your query. As a workaround, you can create aliases of your identifiers [ContactDetails/emailaddress] = [CD/EmAd] for example and replace the values once the XML has been generated.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)