Query Xml in SQL Server 2008 R2 - sql-server-2008-r2

I am new to SQL Server and T-SQL. How can I query out the information stored in this xml in SQL Server 2008 R2?
XML :
<smp:Root xmlns:smp="http://tempuri.org/smp.xsd" header="Test Title">
<smp:Sections>
<smp:G3 idnumber="01">
<SectionHost>ABC</SectionHost>
</smp:G3>
<smp:G2 idnumber="01">
<SectionHost>DEF</SectionHost>
</smp:G2>
</smp:Sections>
</smp:Root>

If you have the Xml stored in a Xml column just use the value method to shred the Xml.
Next time try and post some DDL, DML to show us what you have tried, your table structures etc.
But try this
WITH XMLNAMESPACES (Default 'http://tempuri.org/smp.xsd')
SELECT
a.value('#header', 'nvarchar(50)') as Header,
b.value('local-name(.)', 'nvarchar(50)') as Sections,
b.value('#idnumber' ,'int') as IdNumber,
b.value('.' , 'nvarchar(20)') as Host
From ATable As x
Cross Apply x.AXmlColumn.nodes('Root') a(a)
Cross Apply a.nodes('Sections/*') b(b)
Here are some useful links to get you started:
https://www.simple-talk.com/sql/learn-sql-server/the-xml-methods-in-sql-server/
http://blog.sqlauthority.com/2008/01/15/sql-server-what-is-dml-ddl-dcl-and-tcl-introduction-and-examples/
http://msdn.microsoft.com/en-us/library/ms189254.aspx

Related

Will XQuery work inside a SQL Server 2008 Stored Procedure

Microsoft SQL Server 2008 - Version 10.0.6556.0
;WITH dataAsXml AS
(
SELECT
xmlData.myElement.value('#myAttribute1', 'varchar(10)') AS 'myAttribute1',
xmlData.myElement.value('#myAttribute2', 'char(5)') AS 'myAttribute2'
FROM
#input.nodes('/myRootElement/childElement') AS xmlData(myElement)
)
INSERT INTO myTable (myCol1, myCol2)
SELECT myAttribute1, myAttribute2 FROM dataAsXml
The above xQuery doesn't seem to work from within a stored procedure. The data is definitely there but nothing is inserted into myTable. It works from query window but if the same thing is put inside a stored procedure, nothing seems to happen
<?xml version="1.0" encoding="UTF-8"?>
<myRootElement>
<childElement myAttribute1="Value1" myAttribute2="value2"/>
</myRootElement>

Getting a Result Set's Column Names via T-SQL

Is there a way to get the column names that an arbitrary query will return using just T-SQL that works with pre-2012 versions of Microsoft SQL Server?
What Doesn't Work:
sys.columns and INFORMATION_SCHEMA.COLUMNS work great for obtaining the column list for tables or views but don't work with arbitrary queries.
sys.dm_exec_describe_first_result would be perfect except that this management function was added in SQL Server 2012. What I'm writing needs to be backwards compatible to SQL Server 2005.
A custom CLR function could easily provide this information but introduces deployment complexities on the server side. I'd rather not go this route.
Any ideas?
So long as the arbitrary query qualifies to be used as a nested query (i.e. no CTEs, unique column names, etc.), this can be achieved by loading the query's metadata into a temp table, then retrieving column details via sys.tables:
SELECT TOP 0 * INTO #t FROM (query goes here) q
SELECT name FROM tempdb.sys.columns WHERE object_id = OBJECT_ID('tempdb..#t')
DROP TABLE #t
Thanks to #MartinSmith's for suggesting this approach!

Query referencing aliased column in order by in SQLServer 2012

I have a SQL Select query that's embedded in a piece of C# code which I don't want to change. My problem is that the query executes fine on SQLServer 2008 but not 2012.
The offending line of code is:
Select code as SiteCode from TimeSheetContracts S order by S.SiteCode
Executed in a database on SQL2008 it works fine. The same database upgraded to SQLServer 2012 errors with the following...
Msg 207, Level 16, State 1, Line 2
Invalid column name 'SiteCode'.
If I edit the query to be
Select code as SiteCode from TimeSheetContracts S order by SiteCode
it works fine. Can anyone explain this?
There is no column in TimeSheetContracts called SiteCode, so a reference to s.SiteCode is not valid. Aliasing in ORDER BY is a little more strict since SQL Server 2000, when the syntax was a little more forgiving. The only way s.SiteCode would have worked on your SQL Server 2008 instance was if your database was in COMPATIBILITY_LEVEL = 80 (go ahead and try it on a different database that is 90 or greater). Once you move to SQL Server 2012, 80 is no longer an option. On a 2005, 2008 or 2008 R2 instance, try this:
CREATE DATABASE floob;
GO
USE floob;
GO
CREATE TABLE dbo.SalesOrderHeader(SalesOrderID INT);
GO
SELECT SalesOrderID AS ID FROM dbo.SalesOrderHeader AS h ORDER BY h.ID; -- fails
GO
ALTER DATABASE floob SET COMPATIBILITY_LEVEL = 80;
GO
SELECT SalesOrderID AS ID FROM dbo.SalesOrderHeader AS h ORDER BY h.ID; -- works
GO
USE master;
GO
DROP DATABASE floob;
If you want to use the column alias, you'll need to (and should always have been) just use the alias. If you want to use the table alias prefix, you'll need to use s.code.

Checksum Validation after migration from Oracle to SQL Server

I am migrating a large database from oracle 11g to SQL SERVER 2008R2 using SSIS. How can the data integrity can be validated for numeric data using checksum?
In the past, I've always done this using a few application controls. It should be something that is easy to compute on both platforms.
Frequently, the end result is a query like:
select count(*)
, count(distinct col1) col1_dist_cnt
...
, count(distinct col99) col99_dist_cnt
, sum(col1) col1_sum
...
, sum(col99) col99_sum
from table
Spool to file, Excel or database and compare outcome. Save for project management and auditors.
Please read more on application control here. I wrote it for checks between various financial reporting systems for the regulatory reporting, so this approach serves most cases.
If exactly one field value is wrong, it will always show up. Two errors might compensate each other. For example row 1 col 1 gets the value from row 2 col 1.
To detect for that, multiply each value with something unique for the row. For instance, if you have a unique ID column or identity that is included in the migration too:
, sum(ID * col1) col1_sum
...
, sum(ID * col99) col99_sum
When you get number overflows, first try using the largest available precision (especially on SQL Server sometimes difficult). If not feasibly anymore, use mod function. Only few types of error are hidden by mod.
Icing on the cake is to auto generate these statements. On Oracle look at user_tables, user_tab_columns. On SQL Server look at syscolumns etc.

Simple Way to View SQL Query (ies) Generated by SSRS Reports?

Is there a simple way to view the SQL Queries actually generated by SSRS other than running profile traces to capture them?
Is there some way from within the BIDS editor to see this?
You could run something like the below against your SSRS report server. You will be able to see the sql that is being execute by the report datasets.
;WITH XMLNAMESPACES (
DEFAULT 'http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition',
'http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' AS rd
),
ReportData AS
(
SELECT name ReportName
, x.value('CommandType[1]', 'VARCHAR(50)') AS CommandType
, x.value('CommandText[1]','VARCHAR(8000)') AS CommandText
, x.value('DataSourceName[1]','VARCHAR(50)') AS DataSource
FROM (SELECT name
, CAST(CAST(content AS VARBINARY(MAX)) AS XML) AS reportXML
FROM ReportServer.dbo.Catalog
WHERE content IS NOT NULL
AND type != 3) a
CROSS APPLY reportXML.nodes('/Report/DataSets/DataSet/Query') r(x)
)
SELECT *
FROM ReportData
In short, no. There is no good workaround. However, for development I generally created a test query alongside my work in SSRS. I would edit this inside Management Studio and then just paste the values back into BIDS. Assuming two parameters named "StudentID" and "TeacherID", the query looked like:
DECLARE #StudentID int
DECLARE #TeacherID int
SELECT #StudentID = StudentID FROM Students WHERE StudentName LIKE 'John Doe'
SELECT #TeacherID = TeacherID FROM Teachers WHERE TeacherName LIKE 'Mr. Jones'
-- PASTE IN QUERY FROM BIDS BELOW
This allowed me to use real text values from the drop-down parameter lists and simply paste in my query. Then I could optimize the query in Management Studio and then paste it back into BIDS when I was happy with the result.
What I normally do is run SQL Profiler when I run the report and pull the query out of it with the parameters.
Close the file, change the extension from .rdlc to .rdl and reopen it. It should display as HTML. Now do a search for "select" and there you go!