Will XQuery work inside a SQL Server 2008 Stored Procedure - tsql

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>

Related

Transfer Microsoft Access like statement to postgresql

The original query is for an ms-access db table and contains a like statement which I can not run on the same table transfered to postgresql.
The sql statement:
SELECT Table1.property1, Table1.property2
FROM Table1, Table2
WHERE (((Table2.NAME) Like "*" & [Table1]![property2] & "*"));
property2 is the name of the column. How can I keep the same logic and transfer the sql statement so it works with postgresql?

Stored Procedure datasources in Business Objects server

How is data retrieved via a stored procedure datasource when a report is viewed within Business Objects server? It seems retrieve data when any kind of "SELECT" statement gets executed. But other DML statements in a stored procedure, like inserting to a table, get ignored.
When an application calls a stored procedure, it runs in its entirety. But with Crystal Reports in BO Server, it seems that it like it scrapes the data retrieval code out and runs while ignoring other DML statements.
CREATE PROCEDURE dbo.ExampleTestBlahBlah
AS
BEGIN
INSERT TestTable (RandomColumn)
SELECT 'ABC';
SELECT 'ABC' [ReturnValue]
END
When this query is run in Crystal Reports while ReturnValue is added to the report area, the issue can be seen clearly. The procedure runs correctly in the Preview, but if deployed to a BO server the insert statement gets ignored. Why?
Turns out this behavior was caused by "Save Data with Report" option being checked on my test report. That's all it was. Turn that off and the sprocs work as expected

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.

Query Xml in 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

Faster way to transfer table data from linked server

After much fiddling, I've managed to install the right ODBC driver and have successfully created a linked server on SQL Server 2008, by which I can access my PostgreSQL db from SQL server.
I'm copying all of the data from some of the tables in the PgSQL DB into SQL Server using merge statements that take the following form:
with mbRemote as
(
select
*
from
openquery(someLinkedDb,'select * from someTable')
)
merge into someTable mbLocal
using mbRemote on mbLocal.id=mbRemote.id
when matched
/*edit*/
/*clause below really speeds things up when many rows are unchanged*/
/*can you think of anything else?*/
and not (mbLocal.field1=mbRemote.field1
and mbLocal.field2=mbRemote.field2
and mbLocal.field3=mbRemote.field3
and mbLocal.field4=mbRemote.field4)
/*end edit*/
then
update
set
mbLocal.field1=mbRemote.field1,
mbLocal.field2=mbRemote.field2,
mbLocal.field3=mbRemote.field3,
mbLocal.field4=mbRemote.field4
when not matched then
insert
(
id,
field1,
field2,
field3,
field4
)
values
(
mbRemote.id,
mbRemote.field1,
mbRemote.field2,
mbRemote.field3,
mbRemote.field4
)
WHEN NOT MATCHED BY SOURCE then delete;
After this statement completes, the local (SQL Server) copy is fully in sync with the remote (PgSQL server).
A few questions about this approach:
is it sane?
it strikes me that an update will be run over all fields in local rows that haven't necessarily changed. The only prerequisite is that the local and remote id field match. Is there a more fine grained approach/a way of constraining the merge statment to only update rows that have actually changed?
That looks like a reasonable method if you're not able or wanting to use a tool like SSIS.
You could add in a check on the when matched line to check if changes have occurred, something like:
when matched and mbLocal.field1 <> mbRemote.field1 then
This many be unwieldy if you have more than a couple of columns to check, so you could add a check column in (like LastUpdatedDate for example) to make this easier.