|
Hi all.
I'm trying to use an XML datatype to pass around tabular data between functions, and I'm a bit unfamiliar with both this and XQuery syntax, so this is troublesome for me.
I have a piece of code like so:
DECLARE @time_series XML;
SET @time_series =
'<timeSeries>
<ts><date>2010-06-30</date><value>1.235648</value></ts>
<ts><date>2010-07-31</date><value>564654.235648</value></ts>
<ts><date>2010-08-31</date><value>5465465.235648</value></ts>
<ts><date>2010-09-30</date><value>952031.235648</value></ts>
</timeSeries>'
;
DECLARE @tbl_time_series TABLE(
ts_date DATETIME
, ts_value FLOAT
);
INSERT INTO @tbl_time_series
SELECT
TS.item.nodes('/date').value('.', 'datetime') AS tsdate
, TS.item.nodes('/value').value('.', 'float') AS tsvalue
FROM
@time_series.nodes('timeSeries/ts') AS TS(item)
;
"item", by my reasoning, should allow me to select the date node underneath it in the select, but it appears not.
I get this message:
Msg 227, Level 15, State 1, Line 19
"nodes" is not a valid function, property, or field.
All I want is to end up with a table like this;
ts_date ts_value
---------- --------------
2010-06-30 1.235648
2010-07-31 564654.235648
2010-08-31 5465465.235648
2010-09-30 952031.235648
when I try this:
INSERT INTO @tbl_time_series
SELECT
TS.item.query('/date').value('.', 'datetime') AS tsdate
, TS.item.query('/value').value('.', 'float') AS tsvalue
FROM
@time_series.nodes('timeSeries/ts') AS TS(item)
;
See the query in the select statement instead of the node? this runs alright, but I get a result set like this:
1900-01-01 00:00:00.000 0
1900-01-01 00:00:00.000 0
1900-01-01 00:00:00.000 0
1900-01-01 00:00:00.000 0
I think I'm nearly there but my XQuery noobiness is failing me...
Any suggestions?
|
|
|
|
|
UPDATE
Solved. Here's the final statement.
DECLARE @time_series XML;
SET @time_series =
'<timeSeries>
<ts><date>2010-06-30</date><value>1.235648</value></ts>
<ts><date>2010-07-31</date><value>564654.235648</value></ts>
<ts><date>2010-08-31</date><value>5465465.235648</value></ts>
<ts><date>2010-09-30</date><value>952031.235648</value></ts>
</timeSeries>'
;
DECLARE @tbl_time_series TABLE(
ts_date DATETIME
, ts_value FLOAT
);
INSERT INTO @tbl_time_series
SELECT
TS.item.query('date').value('.', 'datetime') AS tsdate
, TS.item.query('value').value('.', 'float') AS tsvalue
FROM
@time_series.nodes('timeSeries/ts') AS TS(item)
;
SELECT * FROM @tbl_time_series;
Just as I suspected it was my lack of XQuery-fu... d'oh!
Hope someone else finds this useful anyway.
|
|
|
|
|
I seem to be incapable of figuring out how to construct a query to achieve a simple thing. I was given a Microsoft Access table that looks like this: (This is just a simplified example)
RoadID Begin End
----------------------------
01005 0.0 2.3
01005 2.3 4.2
01005 4.2 10.7
01005 12.5 14.4
01005 14.4 16.9
01002 0.0 13.2
01002 13.2 16.2
01002 16.2 17.8
01002 17.8 21.2
02003 0.0 5.5
02003 7.2 10.2
02003 10.2 16.3
02003 16.3 27.8
Is it possible to make a query to find all the records that have a gap between it and the records before or after it (for each RoadID). The order of the original records should be sorted by RoadID and Begin. For this example, the output should be:
RoadID Begin End
----------------------------
01005 4.2 10.7
01005 12.5 14.4
02003 0.0 5.5
02003 7.2 10.2
In the above result, the first line has 10.7 as the End, and the second line has 12.5 as the Begin -- meaning that there is a gap, so both records should be shown. Same with the third and fourth lines. This is done for each RoadID, so for example, even though there is a gap between these two records (16.9 <> 0.0), they should not be shown in the result because their RoadID values are different (01005 and 01002):
01005 14.4 16.9
01002 0.0 13.2
I have tried to use some inner join tricks but could not get a thing. I could do this through a program, but I am curious to know if a single SQL query could achieve it. Any help on this is appreciated. Thanks!
|
|
|
|
|
How should the query react to overlaps?
|
|
|
|
|
Sorry I failed on making this point. The original question should be to find gaps and overlaps. Good point! Thanks!
|
|
|
|
|
Ok, this one works with your example: No it doesn't, it'll give you the combined stretches of road with gaps between them
WITH t1 AS (
SELECT r1.RoadID,r1.BEGIN
FROM Roads r1 Left outer join Roads r2
ON r1.RoadID = r2.RoadID
AND r1.BEGIN > r2.BEGIN
AND r1.BEGIN <= r2.END
GROUP BY r1.RoadID,r1.BEGIN
HAVING Count(r2.BEGIN) = 0
)
,t2 AS (
SELECT r3.RoadID,r3.END
FROM Roads r3 Left outer join Roads r4
ON r3.RoadID = r4.RoadID
AND r3.END >= r4.BEGIN
AND r3.END < r4.END
GROUP BY r3.RoadID,r3.END
HAVING Count(r4.BEGIN) = 0
)
,ContiguousStretch AS (
SELECT t1.RoadID,t1.BEGIN,Min(t2.END) AS END
FROM t1 join t2
ON t1.RoadID = t2.RoadID
AND t1.BEGIN <= t2.END
GROUP BY t1.RoadID,t1.BEGIN
)
,Gaps AS (
SELECT RoadID
FROM ContiguousStretch
GROUP BY RoadID
HAVING Count(RoadID) > 1
)
SELECT c.RoadID,BEGIN,END
FROM ContiguousStretch c join gaps g
ON c.RoadID = g.RoadID
ORDER BY c.RoadID,BEGIN
|
|
|
|
|
|
Take two, this one works with both overlaps and gaps and with no contiguous results:
WITH ordered AS(
SELECT RoadID,BEGIN,END,ROW_NUMBER() OVER(PARTITION BY RoadID ORDER BY BEGIN) as rn
FROM roads
)
SELECT o1.RoadID,o1.BEGIN,o1.END
FROM ordered o1,ordered o2
WHERE o1.RoadID = o2.RoadID
AND o1.rn = o2.rn -1
AND o1.END <> o2.BEGIN
UNION
SELECT o2.RoadID,o2.BEGIN,o2.END
FROM ordered o1,ordered o2
WHERE o1.RoadID = o2.RoadID
AND o1.rn = o2.rn -1
AND o1.END <> o2.BEGIN If you want only the gaps or only the overlaps you vill have to change the conditions in o1.END <> o2.BEGIN
|
|
|
|
|
Thanks, Jörgen! BTW, I have been to Sverige (Göteborg, Stockholm, and Malmö) ten years ago. Beautiful places. I liked it there.
|
|
|
|
|
You could try something like the following
SELECT a.*, b.*
FROM TableName a,
TableName b
WHERE a.roadid = b.roadid
AND b.start = (SELECT MIN(c.start)
FROM TableName c
WHERE c.roadid = a.roadid
AND c.Start > a.start)
AND a.End <> b.Start
ORDER BY ...
|
|
|
|
|
I appreciate your help. Thanks!
|
|
|
|
|
I'm at the last stage of a complete overhaul of our build scripts and I'm down to one last issue.
I can't seem to use sqlcmd to execute a create procedure statement that has a raiserror command in it. sqlcmd pre parses out the command and fails without executing. There doesn't seem to be any documented escape that I can figure out.
|
|
|
|
|
Just made a test with this and didn't have any problems. Could it be that you're having problems with for example apostrophes in the script in the RAISERROR statement.
The test script I used was:
create procedure ptest as
begin
raiserror( 'message', 10, 10 );
end;
go
and the command:
sqlcmd -S .\instancename -E -d dbname -i testproc.txt
Could you post the error message and the part of the script that's causing problems. Also have a test drive with the script above to see if it also causes the same problem.
|
|
|
|
|
I simply searched on here for code to finish a much over due uni ap assignment, now where all this hostility comes from I dont really know, mostly on to finish moi ap tell you the code works, apparently no one gets gleeful when finishing an ap that will progress them into their real life,
I dont see moi self as a programmer nor a even an avid enthusiasts in programming thinking I better post something being that I wasnt planning to even join this jest to getthe code
DO NOT FLOOD MOI EMAIL BOX: WITH INSULTS...
direct Q's that I cannot answer directly plese direct them somewhere else
usernameshelby@hotmail.com
|
|
|
|
|
Well shelby this not an insult but an observation. You posted this in a database forum, I presume you want something to do with a keyboard.
You did not actually post a question just a minor rant about how you were treated.
I also assume you have cross posted this, I suggest you read the forum guidelines on how to post a question to get a sensible answer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
It wasnt a rant I dont know what you mean, besides more of a praise report so I dont get an incomplete! I have to complete jest develop five apps that work that isnt jest
I have a problem with you trying to observe someone like Im in an observatory I cannot approve....I dont know anything pertains to guidelines...
|
|
|
|
|
English is obviously not your strongest language because your sentences do not make sense, I suggest you get someone with reasonable english to help you frame your questions.
See the yellow highlighted post at the top of the page, go read that, it will help you ask a valid question.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Shelby. Which country are you really from? We may be able to get somebody from your country with a fuller grasp of English to help form a question that we can answer.
|
|
|
|
|
Smells like a Troll
I don't speak Idiot - please talk slowly and clearly
'This space for rent'
Driven to the arms of Heineken by the wife
|
|
|
|
|
UH excuse moi I dont know exactly you guys get off topic, this thread jest on the code for the virtual keyboard I dont like to ver off topic, So I dont know what youre trying to say either youre all out to play a mean prank nor a harmless joke...I dont know I care to be apart of...
|
|
|
|
|
hi..
the below is my SP..
i want to pass the parameter @name to the open query
but it is showing the error
incorrect syntax near '+'
ALTER PROCEDURE [dbo].[cub]
@name nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
select * from OPENQUERY(XHMNDGHFYUHFGHJFFRJF,'select {[Employee].[Dept].[Dept]*[Employee].[Empno].[Empno]} on 1,
[Measures].[Salary] on 0 from [Employee] where [Employee].[Empname].&'+ @name +' ')
END
if i pass the value directly..
like this.. where [Employee].[Empname].&vasu ')
it will execute fine..
but through the parameter name it will show error..
help me..
|
|
|
|
|
|
You have several options for this and they all have their downsides. Here's few.
One way is to create the statement and concatenate the parameter values to a string variable. After that you can execute the contents of the variable using EXEC[^]. This may lead to problems when dates, numbers etc are used in the parameters.
Another option is to create a permanent or temporary table and insert the parameter values to this table and join the openquery to this table. Of course you can use other conditions also (exists, 'column returned by openquery' < 'value in temp table' etc etc). Now you can build a data-type safe solution but you may encounter performance problems.
|
|
|
|
|
hi,
when we pass the parameter , not need of the quotes.
for eg:
without param
select * from employee where ename='sss';
with param
Declare en as varchar(5);
set en='wowow';
select * from employee where ename=@en;
Good luck.
|
|
|
|
|
Hi, guys
We encountered a SQL server error message saying, "String or binary data would be truncated". I know it's because some new records to be inserted into a table has value exceed the limit of the column's definition, eg, the value to be inserted is something like "abcdef", but the column's definition is something like "varchar(3)". The problem is, there are more than 10 tables that have been inserted new records in that stored proc, and each has huge amount of columns. Is there a way to let SQL Server tell exactly which table and which column it complains about? So I can avoid the extremely tedious task of using the same problematic data to examine each column?
Thanks very much,
|
|
|
|