Archive
SELECT an XML string to a table – SQL Server
A simple way to SELECT XML string in a tabular format by using Nodes and OPENXML().
–> By using FOR XML AUTO:
USE [AdventureWorks] GO --// Generate an XML result-set: SELECT TOP 5 ContactID, FirstName, LastName FROM Person.Contact FOR XML AUTO GO
Output as XML String: <Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/> <Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/> <Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/> <Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/> <Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/>
–> SELECT by using XML Nodes:
DECLARE @xml XML SET @xml = N' <root><Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/> <Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/> <Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/> <Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/> <Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/></root>' SELECT Tab.Col.value('@ContactID','int') AS ContactID, Tab.Col.value('@FirstName','varchar(20)') AS FirstName, Tab.Col.value('@LastName','varchar(20)') AS LastName FROM @xml.nodes('/root/Person.Contact') Tab(Col) GO
Output:
ContactID FirstName LastName
1 Gustavo Achong
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman
–> SELECT by using OPENXML:
DECLARE @xml XML SET @xml = N' <root><Person.Contact ContactID="1" FirstName="Gustavo" LastName="Achong"/> <Person.Contact ContactID="2" FirstName="Catherine" LastName="Abel"/> <Person.Contact ContactID="3" FirstName="Kim" LastName="Abercrombie"/> <Person.Contact ContactID="4" FirstName="Humberto" LastName="Acevedo"/> <Person.Contact ContactID="5" FirstName="Pilar" LastName="Ackerman"/></root>' DECLARE @docHandle int -- Create internal representation of the XML document and return the xml-doc Handle ID EXEC sp_xml_preparedocument @docHandle OUTPUT, @xml select @docHandle -- 37 SELECT * FROM OPENXML(@docHandle, N'//Person.Contact') WITH (ContactID INT, FirstName VARCHAR(20), LastName VARCHAR(20))
Output:
ContactID FirstName LastName
1 Gustavo Achong
2 Catherine Abel
3 Kim Abercrombie
4 Humberto Acevedo
5 Pilar Ackerman
-- Remove xml-doc Handle EXEC sp_xml_removedocument @docHandle GO
>> Check & Subscribe my [YouTube videos] on SQL Server.
Using SQL_VARIANT datatype to store any datatype value in SQL Server
According to MS BOL SQL_VARIANT can be used in columns, parameters, variables, and the return values of user-defined functions. sql_variant enables these database objects to support values of other data types.
It can have a maximum length of 8016 bytes including both the base-type information and the base-type value. Thus the maximum length of the actual base-type value is 8,000 bytes.
USE [tempdb] GO DECLARE @var SQL_VARIANT -- Set variable as DATETIME type SET @var =GETDATE() SELECT @var as Data, SQL_VARIANT_PROPERTY(@var , 'BaseType') as BaseType , SQL_VARIANT_PROPERTY(@var , 'Precision') as Precision , SQL_VARIANT_PROPERTY(@var , 'Scale') as Scale , SQL_VARIANT_PROPERTY(@var , 'TotalBytes') as TotalBytes , SQL_VARIANT_PROPERTY(@var , 'MaxLength') as MaxLength , SQL_VARIANT_PROPERTY(@var , 'Collation') as Collation;
Output:
Data BaseType Precision Scale TotalBytes MaxLength Collation
2011-01-27 17:13:53.150 datetime 23 3 10 8 NULL
-- Set variable as INT type SET @var = 1234 SELECT @var as Data, SQL_VARIANT_PROPERTY(@var , 'BaseType') as BaseType , SQL_VARIANT_PROPERTY(@var , 'Precision') as Precision , SQL_VARIANT_PROPERTY(@var , 'Scale') as Scale , SQL_VARIANT_PROPERTY(@var , 'TotalBytes') as TotalBytes , SQL_VARIANT_PROPERTY(@var , 'MaxLength') as MaxLength , SQL_VARIANT_PROPERTY(@var , 'Collation') as Collation;
Output:
Data BaseType Precision Scale TotalBytes MaxLength Collation
1234 int 10 0 6 4 NULL
-- Set variable as VARCHAR type SET @var = SYSTEM_USER SELECT @var as Data, SQL_VARIANT_PROPERTY(@var , 'BaseType') as BaseType , SQL_VARIANT_PROPERTY(@var , 'Precision') as Precision , SQL_VARIANT_PROPERTY(@var , 'Scale') as Scale , SQL_VARIANT_PROPERTY(@var , 'TotalBytes') as TotalBytes , SQL_VARIANT_PROPERTY(@var , 'MaxLength') as MaxLength , SQL_VARIANT_PROPERTY(@var , 'Collation') as Collation;
Output:
Data BaseType Precision Scale TotalBytes MaxLength Collation
XXX\mpandey nvarchar 0 0 30 256 Latin1_General_CS_AS
-- Set variable as BIT type SET @var = cast(1 as bit) SELECT @var as Data, SQL_VARIANT_PROPERTY(@var , 'BaseType') as BaseType , SQL_VARIANT_PROPERTY(@var , 'Precision') as Precision , SQL_VARIANT_PROPERTY(@var , 'Scale') as Scale , SQL_VARIANT_PROPERTY(@var , 'TotalBytes') as TotalBytes , SQL_VARIANT_PROPERTY(@var , 'MaxLength') as MaxLength , SQL_VARIANT_PROPERTY(@var , 'Collation') as Collation;
Output:
Data BaseType Precision Scale TotalBytes MaxLength Collation
1 bit 1 0 3 1 NULL
More on SQL_VARIANT: http://msdn.microsoft.com/en-us/library/ms173829.aspx
Creating “Dynamic PIVOT” scripts in SQL Server
My previous PIVOT UNPIVOT post describes to organize data in a cross tab fashion or to transpose columns into rows and vice-versa.
This is fine only when you know how many columns you need. But when columns vary according to the query then how could you apply them on your scripts. The only way is to store them in a string variable at runtime and apply them in a dynamic SQL query, shown below.
This problem was also discussed on MSDN’s following link: http://social.msdn.microsoft.com/Forums/en/transactsql/thread/26f86fd6-6d06-4cc5-9723-9ee4685bd48a where I proposed an answer also described below:
USE [tempdb] GO -- Create test tables create table table1 (number int, desc varchar(20), location int, numberatlocation int) create table table2 (code int, name varchar(20)) -- Insert test data insert into table1 values (12345,'test',1000,5) insert into table1 values (12345,'test',1001,2) insert into table1 values (12345,'test',1002,4) insert into table1 values (12345,'test',1003,9) insert into table1 values (12345,'test',1004,7) insert into table2 values (1000,'loc1') insert into table2 values (1001,'loc2') insert into table2 values (1002,'loc3') insert into table2 values (1003,'loc4') insert into table2 values (1004,'loc5') -- Static PIVOT select number, description, [loc1], [loc2], [loc3], [loc4], [loc5] from (select number, desc, numberatlocation, name from table1 join table2 on table1.location=table2.code)p PIVOT(MAX (numberatlocation) FOR Name IN ( [loc1], [loc2], [loc3], [loc4], [loc5] ) ) AS pvt ORDER BY number
Output of Static query:
number desc loc1 loc2 loc3 loc4 loc5
12345 test 5 2 4 9 7
-- Dynamic PIVOT -- Lets add one more record on both the tables to check the results insert into table1 values (12345,'test',1005,3) insert into table2 values (1005,'loc6') declare @col varchar(1000) declare @sql varchar(2000) select @col = COALESCE(@col + ', ','') + QUOTENAME(name) from table2 select @col -- This gives: [loc1], [loc2], [loc3], [loc4], [loc5], [loc6] -- Now setting this @col variable in the Dynamic SQL. set @sql = ' select number, desc, ' + @col + ' from (select number, desc, numberatlocation, name from table1 join table2 on table1.location=table2.code)p PIVOT(MAX (numberatlocation) FOR Name IN ( ' + @col + ' ) ) AS pvt ORDER BY number' print @sql exec (@sql)
Output of Dynamic query:
number desc loc1 loc2 loc3 loc4 loc5 loc6
12345 test 5 2 4 9 7 3
-- Final Cleanup drop table table1 drop table table2
UNION vs UNION ALL | which one is faster?
UNION and UNION ALL are used to combine data or record sets from two different tables. One could combine more than 2 tables. Just like JOINS, UNION combines data into a single record-set but vertically by adding rows from another table. JOINS combine data horizontally by adding columns from another table.
UNION insures you get DISTINCT records from both the tables.
UNION ALL pulls out all records from both the tables with duplicates.
Generally “UNION ALL” is considered best in performance when compared with UNION.
But you should only use “UNION ALL” when you are sure that you have distinct or no common records in both the records-sets.
For more information on this check my earlier blog post which shows the main differences and how performance differs in b/w the two: https://sqlwithmanoj.wordpress.com/2010/12/30/why-union-all-is-faster-than-union/
SQL Jokes!!!
Yes, SQL Jokes… there are SQL jokes also… on internet that I collated from various sources and now they are in my single post, all below… read & enjoy!!!
>> A SQL query walks into a bar and sees two tables. He walks up to them and says ‘Can I join you?
Then a waitress walks up and says ‘Nice view !
>> Joins are what RDBMS’s do for a living.
>> And afterwards…like most men…he performed a Rollback and never Commited…
>> He picked those two tables after performing a full scan of the other tables in the room.
>> A SQL query walks into a bar on Valentine’s day, and sees two tables. She says “insert all this, you cartesian pigs!”
>> Q: Why do you never ask SQL people to help you move your furniture?
A: They sometimes drops the table
>> The Query was soon surrounded by subQueries – it was then he realized he had walked into an Array Bar.
>> SQL Table walks to a psychiatrist dr. Index
Table: “Doctor, I have a problem”
Dr: “what kind a problem?”
Table: “I’m a mess. I have things all over the place, i always look for my stuff”
Dr. “No problem. I will get you in order”.
>> Index and table are reading a book “index-sutra”
Table: Oh, baby tonight we can try a clustered position”
Index: “yeah baby, we can also try covered position”
Table: “or maybe multiple clustered position”
Index: “baby, yes, that’s the one. i’m just gonna call my friends”
>> Indexianina philosophy on Fill factor 50 = “Half empty or Half full?”
>> What does an execution plan say to t-sql query? “Go f**k yourself, if you are not happy with me”
>> Execution plan to t-sql query is like alter-ego to self.
>> What does table say to a trigger: “Hey, stop it, i’m full”.
>> When did God create the DBA ? A. The day before he had his rights revoked.
>> BIT says to itself: “When I grow up, i want to be BLOB”.
>> There are two types of DBAs:
1) DBAs that do backups
2) DBAs that will do backups
>> An Oracle DBA and a DB2 DBA walk into a bar. The barman asks them what they’d like to drink and a huge debate ensues on how to optimize the query. (Boom Tish)
Their mate the SQL Server DBA rolls in after about 15 minutes only to find them still arguing. After rolling his eyes at them, he walks up to the bar and greets the barman warmly. The barman asks him “Hey, you’re a DBA too aren’t you? Why aren’t you joining in?” The SQL Server DBA grins at the barman and says “Ah… well… the reason I’m late is that this always happens when these clowns go out drinking – I work with SQL Server, so I had the option of optimizing the query using a wizard before I got here! So mine’s a scotch!”
>> NULL is the Chuck Norris of the database – nothing can be compared to it.
>> What kind of undergarments to DBAs wear?
Well, (who’d have guessed it) Depends…
>> It is March 1st and the first day of DBMS school
The teacher starts off with a role call..
Teacher: Oracle?
“Present sir”
Teacher: DB2?
“Present sir”
Teacher: SQL Server?
“Present sir”
Teacher: MySQL?
[Silence]
Teacher: MySQL?
[Silence]
Teacher: Where the hell is MySQL
[In rushes MySQL, unshaven, hair a mess]
Teacher: Where have you been MySQL
“Sorry sir I thought it was February 31st“
… comments & more jokes are welcome!!!
>> Check & Subscribe my [YouTube videos] on SQL Server.