Transact SQL :: Generating XML (serialized) From Query Results
Aug 20, 2015
I didn't work with XML before and so I'm posting this question on how we can generate serialized XML. I have the following table -
DECLARE @Population TABLE (CountryId INT IDENTITY(1,1), CountryName VARCHAR(15), StateName VARCHAR(20), PopulationCount INT)
INSERT INTO @Population (CountryName, StateName, PopulationCount)
VALUES ('USA','California',300000),
('USA','Chicago',500000),
('Australia','Queensland',550000),
[Code] ....
Please note that I can have another country with 10 states or 30 states, so State Name is dynamic. Can this be done in SQL ? or we have to use .NET ?
SQLServer2014
-12.0.2000.8(X64)
View 6 Replies
ADVERTISEMENT
Aug 25, 2004
I am developing a datagrid which will display a summary "report" of sales revenue grouped by a particular field. There will also be a drill-down on this field that will display in more detail the table of data that was used for the summary.
Because the user is selecting multiple criteria under which to run the search, I have created a dynamic query that gathers all the necessary data I need for both the drill down and the summary based on the variable parameters of the user. I've optimized the query that gathers the detailed data (taken from 7 tables) to execute far under a second.
So, that said, here's my question: It seems the most efficient way to get the summary data is to run it against the datatable (which gathers the detailed data) created by the dynamic query and just pass them both into a dataset. Can this be done? Originally, I was calling 2 separate sprocs in my application - one for the detailed data and another for the summary data using a "group by" on the table that was produced by the dynamic query (using "INTO TABLE" in the SELECT statement of the dynamic query). With this method, I received an access exception by asp.net. for the table created by the dynamic query so have dropped the "INTO TABLE", not to mention it increased the performance considerably NOT using the table.
Any suggestion on the most efficient/optimal method to use the data generated from the dynamic query sproc for the summary data? I am trying desparately to avoid running the dynamic query again just for the summary data.
I am a total newbie, so would appreciate any feedback. Also, would someone please tell me the datetime stamp function to print so that I can see how many milliseconds it takes to execute stored procedures?
TIA!
Sandy
View 1 Replies
View Related
Jul 23, 2005
Hi,Im running into a strange problem here!I have created a linked server on one of my SQL Servers (local server).Lets call the linked server as LS1.I have an sproc which resides on the remote server(LS1) and uses a FORXML query.On running this sproc from my local server using linked server LS1, Iget a strange serialized data as output instead of the usual XMLoutput.I run the proc asexec [LS1].[DBName].[UserName].[SprocName] '12/1/2004','12/3/2004'the proc accepts 2 parameters as dates.The output generated is smth like:0x44046E0061006D0065004404440061.......(long string of hex codes)Looks like the ouput is getting serialized. How can I avoidserialization OR How to de-serialize this data?Please HelpThanks,Geo
View 5 Replies
View Related
Jul 30, 2015
For each customer, I want to add all of their telephone numbers to a different column. That is, multiple columns (depending on the number of telephone numbers) for each customer/row. How can I achieve that?
I want my output to be
CUSTOMER ID, FIRST NAME, LAST NAME, TEL1, TEL2, TEL3, ... etc
Each 'Tel' will relate to a one or more records in the PHONES table that is linked back to the customer.
I want to do it using SELECT. Is it possible?
View 13 Replies
View Related
Jul 10, 2015
I have a query that performs a comparison between 2 different databases and returns the results of the comparison. It returns 2 columns. The 1st column is the value of the object being compared, and the 2nd column is a number representing any discrepancies.What I would like to do is use the results from this 1st query in the where clause of another separate query so that this 2nd query will only run for any primary values from the 1st query where a secondary value in the 1st query is not equal to zero.I was thinking of using an "IN" function in the 2nd query to pull data from the 1st column in the 1st query where the 2nd column in the 1st query != 0, but I'm having trouble ironing out the correct syntax, and conceptualizing this optimally.
While I would prefer to only return values from the 1st query where the comparison value != 0 in order to have a concise list to work with, I am having difficulty in that the comparison value is a mathematical calculation of 2 different tables in 2 different databases, and so far I've been forced to include it in the select criteria because the where clause does not accept it.Also, I am not a DBA by trade. I am a system administrator writing SQL code for reporting data from an application I support.
View 6 Replies
View Related
Sep 16, 2015
After running a query (from the Query Builder) in SQL Server 2008 sometimes I can right-click on the results pane and "Save results as CSV file", other times it's not an option. After running a query for 24 hours (several million record results) I can't seem to do anything with the results. I have my settings:
Options | Query results | SQL Server | Default Destination for Results
set to "Results to File" and a path entered, but it doesn't work. Is there wording I can add to the end of my SQL statement such as "TO FILE xxx.csv" or something?
View 4 Replies
View Related
Aug 8, 2015
I’m running a data integrity procedure from an agent job that is a scheduled weekly maintenance task which emails the results of my query, and is working properly.
I would however like this to only receive the email it the query contains results, and not send it no records are found to prompt me to take action.
My code less the personal information
EXEC msdb.dbo.sp_send_dbmail
@profile_name
= '',
@recipients
= '',
@subject
= 'Database Integrity - Import Errors',
[Code] ....
View 5 Replies
View Related
Jun 16, 2015
I have three tables, Accounts, AccountCustomer and Customers, and the data-relationshiop between are defined according to the image below:
I created also a query (the sql-query below), displaying the customers for every account that is on the table "Accounts", and I got the results, as we can see in the image below:
SELECT A.AccountID,
c.CustomerNo,
c.Surname,
c.Name,
c.TaxNum
FROM Accounts A
left join AccountCustomer ac on ac.AccountID = A.AccountID
left join Customers c on c.CustomerNo = ac.CustomerNo
order by A.AccountID;
As we understand, an "AccountID" have multiple customers, so I want to transform tha multiple results to one row, grouping by AccountID (one account belongs to one or many Customers), like the image below:
I tried to use row_number()-expression to get this, but I didn't make it. So my question is, how can I alter my sql-query to get the final result like image above?
View 6 Replies
View Related
Jun 17, 2015
I pulled some examples of using a subquery pivot to build a temp table, but cannot get it to work.
IF OBJECT_ID('tempdb..#Pyr') IS NOT NULL
DROP TABLE #Pyr
GO
SELECT
vst_int_id,
[4981] AS Primary_Ins,
[4978] AS Secondary_Ins,
[code]....
The problems I am having are with the integer data being used to create temp table fields. The bracketed numbers on line 7-10 give me an invalid column name error each. In the 'FOR', I get another error "Incorrect syntax near 'FOR'. Expecting '(', or '.'.". The first integer in the "IN" gives me an "Incorrect syntax near '[4981]'. Expecting '(' or SELECT". I will post the definitions from another effort below.
CREATE TABLE #Pyr
(
vst_int_idINTEGERNOT NULL,
--ivo_int_idINTEGERNOT NULL,
--cur_pln_int_idINTEGERNULL,
--pyr_seq_noINTEGERNULL,
[code]....
SQL Server 2008 R2.
View 3 Replies
View Related
May 21, 2015
I have a data structure like this
UID , Name, amount, start date End Date
1 A 10 2015-05-01 00:00:00 2015-05-01 23:59:59
2 A 10 2015-05-02 00:00:00 2015-05-02 23:59:59
3 A 10 2015-05-03 00:00:00 2015-05-03 23:59:59
4 A 10 2015-05-04 00:00:00 2015-05-04 23:59:59
5 B 10 2015-05-05 00:00:00 2015-05-05 23:59:59
[code]...
View 5 Replies
View Related
May 14, 2015
We created sql alerts on all our sql servers environments. Now, i want to see each sql server which sql alerts so far got fired and which one never occurs. is there any way, we can get this information from any system database?
View 9 Replies
View Related
Jul 9, 2015
I am trying to fetch records from excel sheet using Select Query but I am getting the error message saying
"Msg 7302, Level 16, State 1, Line 1
Cannot create an instance of OLE DB provider "Microsoft.Jet.OLEDB.4.0" for linked server "(null)"."
Here is my Query,
sp_configure 'show advanced options',1
reconfigure with override
go
sp_configure 'Ad Hoc Distributed Queries',1
reconfigure with override
go
reconfigure
SELECT *
FROM OPENROWSET
('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=C:M2MworkedworkedBOS.xlsx;HDR=YES', 'select * from [Sheet1$]') AS A;
View 5 Replies
View Related
Oct 1, 2015
I have a query that returns the data about test cases. Each test case can have multiple bugs associated to it. I would like a query that only returns the test cases that have all their associated bugs status = closed.For instance here is a sample of my data
TestCaseID TestCaseDescription BugID BugStatus
1 TestCase1 1 Closed
2 TestCase1 2 Open
3 TestCase2 11 Closed
4 TestCase2 12 Closed
5 TestCase2 13 Closed
How can I limit this to only return TestCase2 data since all of that test case's bugs have a status of closed.
View 3 Replies
View Related
Oct 28, 1999
I wrote a stored procedure with three variables to be passed as input parameters. The stored procedure runs a select into statement into a temp table. The resulting temp table with another table was queried using right outer join to produce the desired results.
The stored procedure compiles error free.However when I ran the stored procedure with the parameters(3) in ISQL/W (SQL Server 6.5) the headers(column-names) were displayed but no records.
When runned as a query with the same parameter values, records were produced.
Help please urgent.
Regards
Olutimi Ooruntoba
View 2 Replies
View Related
Mar 16, 2015
I am rewriting several stored procedures that originally had lots of "multiplicated" code. I am aware that references to objects within dynamic SQL do not create dependencies, so I intend to add code that will generate the dependencies but will NOT produce any output in both the Results and Messages tabs, not be overly "messy" or complicated, and have the least impact on execution plan creation as possible.
As we use a dependency list of tables used to our support staff pinpoint possible data issues associated with each of these stored procedures.
I have tried a few methods already, including this:
SET @SQL = N'SELECT Column1,Column2 FROM dbo.TableName';
...
/***************************************************************************************/
/* This code block is only to establish dependency of objects used within dynamic SQL. */
/* */
/* SET statements are used so that no output is produced in Results or Messages tabs. */
/* Object existence check avoids error 208, "Invalid object name" message. */
/***************************************************************************************/
DECLARE @DependentObject SQL_VARIANT;
IF OBJECT_ID(N'dbo.TableName', N'U') IS NOT NULL
SET @DependentObject = ( SELECT TOP (1) Column1,Column2 FROM dbo.TableName);
/* End code for dependency of objects used within dynamic SQL */
View 2 Replies
View Related
Apr 1, 2007
hi, like, if i need to do delete some items with the id = 10000 then also need to update on the remaining items on the with the same idthen i will need to go through all the records to fetch the items with the same id right? so, is there something that i can use to hold those records so that i can do the delete and update just on those records and don't need to query twice? or is there a way to do that in one go ?thanks in advance!
View 1 Replies
View Related
Feb 12, 2008
Hello. I currently have a website that has a table on one webpage. When a record is clicked, the primary key of that record is transfered in the query string to another page and fed into an sql statement. In this case its selecting a project on the first page, and displaying all the scripts for that project on another page. I also have an additional dropdownlist on the second page that i use to filter the scripts by an attribute called 'testdomain'. At present this works to an extent. When i click a project, i am navigated to the scripts page which is empty except for the dropdownlist. i then select a 'testdomain' from the dropdownlist and the page populates with scripts (formview) for the particular test domain. what i would like is for all the scripts to be displayed using the formview in the first instance when the user arrives at the second page. from there, they can then filter the scripts using the dropdownlist.
My current SQL statement is as follows.
SelectCommand="SELECT * FROM [TestScript] WHERE (([ProjectID] = @ProjectID) AND ([TestDomain] = @TestDomain))"
So what is happening is when testdomain = a null value, it does not select any scripts. Is there a way i can achieve the behaivour of the page as i outlined above? Any help would be appreciated.
Thanks,
James.
View 1 Replies
View Related
Feb 19, 2008
Hi
is it possible to store a SQL CE command object in SQL Server CE database.
If so then what would be the datatype of such column????
thanx in advance
View 1 Replies
View Related
Mar 27, 2002
Hi Everybody,
I am getting this error when I try to bcp in or even when
I try to create a Primary key or index on a Table.
- Unable to create index 'PK_CMCMessages'.
ODBC error: [Microsoft][ODBC SQL Server Driver][SQL Server]
Backup, CHECKALLOC, bulk copy, SELECT INTO, and file
manipulation (such as CREATE FILE) operations on a
database must be serialized. Reissue the statement
after the current backup, CHECKALLOC, or file manipulation
operation is completed.
[Microsoft][ODBC SQL Server Driver][SQL Server]Could not
create constraint. See previous errors.
I ran CHECKALLOC and its running for days and I dont have
any clue whats happening. If you have any idea please let
me know how to solve this problem.
tks in advance,
Sri
View 4 Replies
View Related
Apr 3, 2007
Hi!
We are trying to queue a xml serialized object to a servive broker queue on a conversation.
The object is a simple .net c# class with few properties mostly string, which we serialize for queing and de/serialize after de-queing. Everything works as expected. Except that when the object contains language characters such as Æ Å Ø, the SEND ON CONVERSATION seems to fail. We are using UTF-8 encoding.
Below is part of the code.
public override bool QueueMessage(EmailMessage message)
{
if (message != null)
{
string xml = "";
using (MemoryStream stream = new MemoryStream())
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(message.GetType());
x.Serialize(stream, message);
xml = ConvertByteArrayToString(stream.ToArray());
}
string sql =
string.Format (@"DECLARE @dialog_handle UNIQUEIDENTIFIER;
BEGIN DIALOG CONVERSATION @dialog_handle
FROM SERVICE [MAINPostingService]
TO SERVICE 'MAINService'
ON CONTRACT [MAINContract] ;
SEND ON CONVERSATION @dialog_handle
MESSAGE TYPE [MAINMessageType]
('{0}') ;
End Conversation @dialog_handle
With cleanup", xml);
string connectionString = ConfigurationManager.ConnectionStrings["MessagingSystemConnectionString2005"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = null;
try
{
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.Transaction = conn.BeginTransaction();
cmd.ExecuteNonQuery();
cmd.Transaction.Commit();
conn.Close();
return true;
}
.
.
}
}
//
private string ConvertByteArrayToString(byte[] byteArray)
{
Encoding enc = Encoding.UTF8;
string text = enc.GetString(byteArray);
return text;
}
Kind Regards
BGSK
Bangalore
India
View 2 Replies
View Related
May 12, 2008
I've read some articles on generating tag clouds but so far only one actually showed a query. And the query was on a simple database with a few tags. In our database we have records that can have literally hundreds of unique tags. So, I'm wondering how to get a realistic result set of the top tags? I can't just do a group by and select the top 30 or 50 because that won't necessarily get me through the alphabet. I'd like to at least have as many letters from the alphabet in the tags as possible like (ant, cat, dog, horse, lion, seal, zebra) but if I do the top 30 it may cut off around the letter "c". Know what I mean? So, how can I generate an accurate tag cloud query that uses all available letters from the alphabet as starting letters for the tags? Thanks.
View 5 Replies
View Related
Sep 22, 2004
Hi everyone,
I am having a table with which i generate a report. Now how to using a SQL query i can generate a auto incrementing no.
Say i am executing this query
SELECT NAME,AGE,ADDRESS FROM MEMBER which gives
NAME AGE ADDRESS
HOLYMAC 13 MALACCA
HOLYCOW 25 USA
HOLYGOD 55 LONDON
Now how can i make it come out like this
SNO NAME AGE ADDRESS
1HOLYMAC 13 MALACCA
2HOLYCOW 25 USA
3HOLYGOD 55 LONDON
See the first column is a auto incrementing number 1,2,3.
How can I write a SQL QUERY that outputs a auto incrementing number.
Thank you
Have nice day
View 2 Replies
View Related
Apr 11, 2008
If I wanted to run a query on any table and in the recordset that is returned have an 'id' field (or whatever) with the record id of that record, how would I do this?
I'm thinking something like
Select field1, field2, recordNumber // derived somehow - not an actual field
from table
where the result woule be:
field1 field2 1
field1 field2 2
field1 field2 3
field1 field2 4
field1 field2 5
...
View 6 Replies
View Related
Jun 24, 2015
I have a table named "Persons" which has the following columns [ Name ,Age, City]. I have a Windows app which has 3 textboxes for each column. My requirement is when I enter only name and enter the search button, I need result similar to this query.
select * from persons where name like '@tbname%'
when I enter only age and enter the search button, I need result similar to this query.
select * from persons where age =@tbage
when I enter name and city, I need result similar to this query.
select * from persons where name like '@tbname%' and city like '@tbcity%'
If I don't enter any fields and enter the serach button, I need result similar to this query.
select * from persons for these 3 fields I might give any combination as above. But how can I have a single query with all these combinations. I want a single query capable of doing this. I cannot have query like this, select * from persons where name like 'tbname%' and city like 'tbcity%' and age =@age
Because if I give only name, city is considered as null nad age is considered as 0 and I get empty result. I cannot have Stored proc and I cannot do much in C# like using "selectqueryBuilder" etc. I need pure SQL query.
View 4 Replies
View Related
Sep 4, 2015
I am working on an HTTP POST application where I am using SQL to return "message" based text results.
A typical query would yield a message something like:
135,15,1,1,1,4,1,1,1,"NAME"
I now have a requirement to have the same query return several rows, but when i send them, they need to be a single message containing several records delimited by CRLF, yielding (1) message, so the result set would look like:
135,15,1,1,1,4,1,1,1,"NAME"
135,15,2,1,1,2,2,3,2,"TestName"
135,15,3,1,1,4,1,1,1," 41"
135,15,4,1,1,4,1,1,1,"asd"
how to do this?
View 3 Replies
View Related
Jan 23, 2008
I hope I'm posting this in the correct forum. If not I apologize. I have a nested select query that I imported from Oracle:
Oracle Version:
Code Snippetselect avg(days) as days from (
select dm_number, max(dm_closedate) - max(comment_closed_date) as days from dm_data
where
dm_type = 'prime' and
dm_closedate <= '31-dec-2007' and
dm_closedate >= '1-dec-2007' and
program = 'aads'
group by dm_number)
SQL Version:
select round(abs(avg(days)), 0) as days from
(select dm.dm_number, abs(datediff(DAY,max(dm.dm_closedate), max(dm.comment_closed_date))) as days
from dm_data dm, ProgramXref px
where
px.Program_Name = 'aads'
and dm.Program_Id = px.Program_Id
and dm.dm_type = 'prime'
and dm.dm_closedate <= '31-dec-2007'
and dm.dm_closedate >= '1-dec-2007'
group by dm.dm_number)
In Oracle the query runs fine. In SQL I am getting a "Line 10: Incorrect syntax near ')'." error. If I run just the nested portion of the query, there are no errors. It only happens when the first query tries to query the nested query. Can anyone help me get the syntax correct?
Thanks,
Lee
View 4 Replies
View Related
Oct 5, 2015
We have two tables.
Table1:
Servers | Numbers
------------
Server1 | 1
Server1 | 2
Server1 | 3
Server2 | 1
Server2 | 2
Server2 | 4
Server3 | 2
Server3 | 5
Server3 | 9
Server3 | 7
.....
Table2:
| Numbers
-----------
NULL | 1
NULL | 2
NULL | 3
I need to select Server1, Server2, Server3 and other servers that does not have correct value in Table2. Results should return server name and number that server does not have like:
Server2 | 3
Server3 | 1
Server3 | 3
Table1 is updated time to time, Table2 - static table. The best would be to avoid loop or cursor. Is that possible to get these results in one query?
View 4 Replies
View Related
Jul 8, 2015
I needed to add in the Fiscal Year (FY) to group my results by FY. I changed my code to the following:
/*
EXEC ADAMHsp_BSS_GetNonMedicaidReportTotals
@pstrProviderName = 'FAM SER-WOOD'
*/
ALTER PROCEDURE [dbo].[ADAMHsp_BSS_GetNonMedicaidReportTotals]
@pstrProviderNameVARCHAR(100)
AS
BEGIN
[Code] ....
See my result set in the picture below. The rows with NULL in the 'ProcGrp' column have the totals of the groupings by FY that I am looking for - that's great. What I want to do now is have another row that contains the sums of the values from any row where 'ProcGrp' is null so that I have a totals row.
View 3 Replies
View Related
Nov 9, 2015
I have a table similar to below:
itemID | part
1 | A
1 | B
2 | A
2 | A
2 | A
3 | C
I need the table to look like the following:
itemID | part1 | part2 | part 3
1 | A | B | null
2 | A | A | A
3 | C | null | null
There will _never_ be more than three parts to an item, and it does not matter what order they are in.
I cannot get pivot to work for me.
View 2 Replies
View Related
May 28, 2015
I am trying to create a proc and at the end of the proc I want to call another proc and pass to one of the parameters to proc using the result from the "OUTPUT". Is it possible to use the results from the "OUTPUT" and use them as parameters?
USE [MyDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
[code]....
View 16 Replies
View Related
May 27, 2015
How can I return results from this SP?
Alter
Procedure sp_Blocking
as
SET NOCOUNT
ON
truncate
table blocked
[Code] ....
View 4 Replies
View Related
Jun 4, 2015
I have a query that pulls back task and user assigned. Each task can have multiple users assigned. I want to pull back the single task and all the users assigned in one row.
Current Query:
select
t.Name 'Task',
d.FirstName + d.LastName 'User'
from [dbo].[Tasks_TemplateAssignTo] a join
Task_Template t on a.template_id = t.ID join
Doctor d on d.id = a.provider_id
Results from query above:
TaskUser
Call CustomerJohn Smith
Call CustomerBetty White
Call CustomerTammy Johnson
Order suppliesGreg Bullard
Order suppliesJosephine Gonzalez
Expected Results:
TaskUser
Call CustomerJohn Smith, Betty White, Tammy Johnson
Order SuppliesGreg Bullard, Jospehine Gonzalez
View 4 Replies
View Related
Jul 24, 2015
Update statement based on the results from this SELECT:
SELECT RELQ_REL_VERSION_NM,
RELQ_RELEASE_Q_ID,
RELQ_STATUS_CD,
RELSP_RELEASE_STEP_ID,
RELSP_STEP_TYPE_CD,
RELSP_STATUS_CD
FROM RELEASE_QUEUE WITH (NOLOCK)
[Code] ....
I need to update all records where the
RELEASE_STEPS.RELSP_STATUS_CD = 'SKPD'TORELEASE_STEPS.RELSP_STATUS_CD = 'CMPS'
I imagine that the UPDATE statement will be something like:
UPDATE RELEASE_STEPS SET dbo.RELEASE_STEPS.RELSP_STATUS_CD = 'CMPS'
WHERE (
SELECT RELQ_REL_VERSION_NM,
RELQ_RELEASE_Q_ID,
RELQ_STATUS_CD,
RELSP_RELEASE_STEP_ID,
RELSP_STEP_TYPE_CD,
[Code] .....
View 4 Replies
View Related