Showing posts with label output. Show all posts
Showing posts with label output. Show all posts

Monday, March 26, 2012

Parent to Child mappings identification in a single query

Hi,

I want to retrive the output in a single SQL query.

I had two columns in a table; I used some values of column1 in column2.
I want to identify all the column1 vlaues in Left Hand side and column2 values in Right Hand side, with a single query.

example:

MasterTable

ParentRow ChildRow
--
A B
C D
E F
G C
H E
M G


Output required:

ParentRow ChildRow
--
A B
C D
E F
G D (C mapped tfrom D)
H F
M D (G mapped from C, and C mapped from D)

To get the above output, I written query using WHILE...[BREAK|CONTINUE] concept until I get @.@.rowcount become zero.

I am looking for better performace and to get the output in a single query, instead of multiple passes.

Can any one suggest good tools and articles on performance tuning techniques?

Thanks
Sreekanth

Waht is your database? SQL Server 2000 or 2005?

|||

If you use SQL Server 2005,

Code Snippet

Create Table #data (

[ParentRow] char ,

[ChildRow] char

);

Insert Into #data Values('A','B');

Insert Into #data Values('C','D');

Insert Into #data Values('E','F');

Insert Into #data Values('G','C');

Insert Into #data Values('H','E');

Insert Into #data Values('M','G');

;WITH TREE

as

(

Select

[ParentRow],

[ChildRow],

[ChildRow] as [Alternate],

Cast('*' + '\' + '*' as varchar(max)) Path

from #Data

Union ALL

Select

data.[ParentRow],

data.[ChildRow],

Tree.[Alternate],

Cast(Path + '\' + '*' as varchar(max)) Path

from #Data as data

Join Tree On Data.[ChildRow] = Tree.[ParentRow]

)

,Tree2 as

(

Select

[ParentRow],

[ChildRow],

[Alternate],

Len(path) as PathLen,

Max(Len(path)) Over(Partition BY [ParentRow], [ChildRow]) MaxLen

from Tree

)

select

[ParentRow],

[Alternate]

from

Tree2

Where

PathLen=MaxLen

Drop table #data

|||

If you use SQL server 2000,

Code Snippet

create table datatable (

[parentrow] char ,

[childrow] char

);

insert into datatable values('A','B');

insert into datatable values('C','D');

insert into datatable values('E','F');

insert into datatable values('G','C');

insert into datatable values('H','E');

insert into datatable values('M','G');

go

create function findalternate(@.childvalue char)

returns char

as

begin

declare @.return as char;

select @.return = childrow from datatable where parentrow=@.childvalue;

if @.return is null

return @.childvalue

else

return dbo.findalternate(@.return)

return null;

end

go

select *, dbo.findalternate(childrow) from datatable

|||

I am using SQL 2005 Server.

Thank you for providing the T-SQL.

Wednesday, March 21, 2012

Parse output from xp_cmdshell

I would like to write the output from (Exec master..xp_cmdshell 'dir
E:\NewOrleans\*FULL.BAK') to a variable. I would like to test each line of
the output variable to see if (wildcard.BAK) exist. If (wildcard.BAK) exis
t
I append this code to my existing script.
Please help me create this script.
Thanks,
-- directory full backup listing
Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK'
Output from (Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK')
Volume in drive E is NWORLBD01A Dumps
Volume Serial Number is DRV-73B6
NULL
Directory of E:\NewOrleans
NULL
05/12/2005 12:57 AM 168,988,440,064 STARDEV_db_200505152100.BAK
1 File(s) 168,968,064 bytes
0 Dir(s) 295,359,264 bytes free
NULLJoe K. wrote:
> I would like to write the output from (Exec master..xp_cmdshell 'dir
> E:\NewOrleans\*FULL.BAK') to a variable. I would like to test each
> line of the output variable to see if (wildcard.BAK) exist. If
> (wildcard.BAK) exist I append this code to my existing script.
> Please help me create this script.
> Thanks,
> -- directory full backup listing
> Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK'
>
> Output from (Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK')
> Volume in drive E is NWORLBD01A Dumps
> Volume Serial Number is DRV-73B6
> NULL
> Directory of E:\NewOrleans
> NULL
> 05/12/2005 12:57 AM 168,988,440,064 STARDEV_db_200505152100.BAK
> 1 File(s) 168,968,064 bytes
> 0 Dir(s) 295,359,264 bytes free
> NULL
Create Table #output (output varchar(1000))
insert into #output
Exec master..xp_cmdshell 'dir C:'
Select * from #output
drop table #output
David Gugick
Imceda Software
www.imceda.com|||Look at this example:
create table #ipconfig(line varchar(2000))
insert into #ipconfig
execute xp_cmdshell 'ipconfig.exe'
select line from #ipconfig
drop table #ipconfig
YOu can "parse" that table with
Select * form #ipconfig where line like '%Something%'
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"Joe K." <Joe K.@.discussions.microsoft.com> schrieb im Newsbeitrag
news:4D55228E-D4E2-4EA0-A533-1B4556C5232F@.microsoft.com...
> I would like to write the output from (Exec master..xp_cmdshell 'dir
> E:\NewOrleans\*FULL.BAK') to a variable. I would like to test each line
> of
> the output variable to see if (wildcard.BAK) exist. If (wildcard.BAK)
> exist
> I append this code to my existing script.
> Please help me create this script.
> Thanks,
> -- directory full backup listing
> Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK'
>
> Output from (Exec master..xp_cmdshell 'dir E:\NewOrleans\*FULL.BAK')
> Volume in drive E is NWORLBD01A Dumps
> Volume Serial Number is DRV-73B6
> NULL
> Directory of E:\NewOrleans
> NULL
> 05/12/2005 12:57 AM 168,988,440,064 STARDEV_db_200505152100.BAK
> 1 File(s) 168,968,064 bytes
> 0 Dir(s) 295,359,264 bytes free
> NULL
>|||Create temp table and change my path to you path and it will give you
directory returned data in a table then you extract the peaces you need.
Create TABLE #temp
(
data nvarchar (1000)
)
Insert #temp
Exec master..xp_cmdshell 'dir \\server\backup\sql\FULL_Backup\*(FULL)*
.BKP'
select * from #temp
"Jens Sü?meyer" wrote:

> Look at this example:
> create table #ipconfig(line varchar(2000))
> insert into #ipconfig
> execute xp_cmdshell 'ipconfig.exe'
> select line from #ipconfig
> drop table #ipconfig
>
> YOu can "parse" that table with
> Select * form #ipconfig where line like '%Something%'
> --
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
> "Joe K." <Joe K.@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:4D55228E-D4E2-4EA0-A533-1B4556C5232F@.microsoft.com...
>
>

Tuesday, March 20, 2012

ParametersUsed Always Polulated after calling ReportingService.Ren

In SSRS 2000 SP2 the documentation for ReportingService.Render states that
the ParametersUsed output parameter should only be populated only if the
report being rendered is a report history snapshot. I'm invoking the Render
method with a null HistoryId as follows, so I would expect the ParametersUsed
output parameter not to get populated.
results = m_RptService.Render(m_RptCatalogItem.Path /*Report*/,
m_Format /*Format*/,
null /*HistoryID*/,
null /*DeviceInfo*/,
m_RptParameters /*Parameters*/,
null /*Credentials*/,
null /*ShowHideToggle*/,
out encoding /*Encoding*/,
out mimeType /*MimeType*/,
out historyParms /*ParametersUsed*/,
out warnings /*Warnings*/,
out streamIDs /*StreamIds*/);
The call generates the following HTTP traffic. As you can see the HistoryId
is not sent in the request yet ParametersUsed is populated in the result. If
I'm interpreting the documentation correctly this value should not be
populated in the response. I really don't what ParametersUsed to be
populated in the response. Is this the correct behavior?
POST /ReportServer/ReportService.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction:
"http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices/Render"
Content-Length: 524
Expect: 100-continue
Host: localhost
Cookie: sqlAuthCookie=****; UsrTkn=>****; AppTkn=>****
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><Render
xmlns="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><Report>/ReportServerStatistics/PTest</Report><Format>HTML4.0</Format><Parameters><ParameterValue><Name>p1</Name><Value>2/11/2007
7:32:50
PM</Value></ParameterValue></Parameters></Render></soap:Body></soap:Envelope>
HTTP/1.1 200 OK
Date: Mon, 12 Feb 2007 00:32:50 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 9021
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Header><ServerInfoHeader
xmlns="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><ReportServerVersionNumber>Microsoft
SQL Server Reporting Services Version
8.00.1038.00</ReportServerVersionNumber><ReportServerEdition>Enterprise</ReportServerEdition></ServerInfoHeader><SessionHeader
xmlns="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><SessionId>glbci3rl3utt1iqmon253t45</SessionId><IsNewExecution>true</IsNewExecution><ExecutionDateTime>2007-02-11T19:32:50</ExecutionDateTime><ExpirationDateTime>2007-02-11T19:32:50</ExpirationDateTime></SessionHeader></soap:Header><soap:Body><RenderResponse
xmlns="http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><Result>
Base 64 Encoded response removed to shorten post
</Result><Encoding>Unicode
(UTF-8)</Encoding><MimeType>text/html</MimeType><ParametersUsed><ParameterValue><Name>p1</Name><Value>2/11/2007
7:32:50 PM</Value></ParameterValue></ParametersUsed><StreamIds
/></RenderResponse></soap:Body></soap:Envelope>Reporting Services 2005 exhibits the same behavior. The following
application produces the following output "Length: 3" indicating that the
ParametersUsed output parameter was populated even though no histryid was
provided. Is this the correct behavior? If so how do I turn it off?
According to the following documentation this does not seem like the correct
behavior (http://msdn2.microsoft.com/en-us/library/aa258532(SQL.80).aspx).
Notice that the description for ParametersUsed states that the output
parameter is only populated of a history snapshot is being rendered. This
causes fairly significant performance issues for me because in some cases I
am passing large parameters. Instead of these parameters just being in the
request, they are in both the request and the response causing the response
to be much larger than I desire.
using System;
using System.Collections.Generic;
using System.Text;
using ConsoleApplication9.rs;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
string encoding, mimeType;
ParameterValue[] pout;
Warning[] w;
string[] s;
ReportingService rs = new ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
ParameterValue[] pin = new ParameterValue[3];
pin[0] = new ParameterValue();
pin[0].Name = "stdt";
pin[0].Value = "2/19/2007";
pin[1] = new ParameterValue();
pin[1].Name = "enddt";
pin[1].Value = "2/20/2007";
pin[2] = new ParameterValue();
pin[2].Name = "sopco";
pin[2].Value = "A";
rs.Render("/VRU Reports/VRUAppSuccessPercent" /*Report*/,
"PDF" /*Format*/,
null /*HistoryID*/,
null /*DeviceInfo*/,
pin /*Parameters*/,
null /*Credentials*/,
null /*ShowHideToggle*/,
out encoding /*Encoding*/,
out mimeType /*MimeType*/,
out pout /*ParametersUsed*/,
out w /*Warnings*/,
out s /*StreamIds*/);
Console.WriteLine("Length: " + pout.Length);
Console.ReadLine();
}
}
}
"Jerry" wrote:
> In SSRS 2000 SP2 the documentation for ReportingService.Render states that
> the ParametersUsed output parameter should only be populated only if the
> report being rendered is a report history snapshot. I'm invoking the Render
> method with a null HistoryId as follows, so I would expect the ParametersUsed
> output parameter not to get populated.
> results = m_RptService.Render(m_RptCatalogItem.Path /*Report*/,
> m_Format /*Format*/,
> null /*HistoryID*/,
> null /*DeviceInfo*/,
> m_RptParameters /*Parameters*/,
> null /*Credentials*/,
> null /*ShowHideToggle*/,
> out encoding /*Encoding*/,
> out mimeType /*MimeType*/,
> out historyParms /*ParametersUsed*/,
> out warnings /*Warnings*/,
> out streamIDs /*StreamIds*/);
> The call generates the following HTTP traffic. As you can see the HistoryId
> is not sent in the request yet ParametersUsed is populated in the result. If
> I'm interpreting the documentation correctly this value should not be
> populated in the response. I really don't what ParametersUsed to be
> populated in the response. Is this the correct behavior?
> POST /ReportServer/ReportService.asmx HTTP/1.1
> Content-Type: text/xml; charset=utf-8
> SOAPAction:
> "http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices/Render"
> Content-Length: 524
> Expect: 100-continue
> Host: localhost
> Cookie: sqlAuthCookie=****; UsrTkn=>****; AppTkn=>****
> <?xml version="1.0" encoding="utf-8"?><soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="<soap:Body><Render">http://www.w3.org/2001/XMLSchema"><soap:Body><Render
> xmlns="<Report>/ReportServerStatistics/PTest</Report><Format>HTML4.0</Format><Parameters><ParameterValue><Name>p1</Name><Value>2/11/2007">http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><Report>/ReportServerStatistics/PTest</Report><Format>HTML4.0</Format><Parameters><ParameterValue><Name>p1</Name><Value>2/11/2007
> 7:32:50
> PM</Value></ParameterValue></Parameters></Render></soap:Body></soap:Envelope>
> HTTP/1.1 200 OK
> Date: Mon, 12 Feb 2007 00:32:50 GMT
> Server: Microsoft-IIS/6.0
> X-Powered-By: ASP.NET
> X-AspNet-Version: 1.1.4322
> Cache-Control: private, max-age=0
> Content-Type: text/xml; charset=utf-8
> Content-Length: 9021
> <?xml version="1.0" encoding="utf-8"?><soap:Envelope
> xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="<soap:Header><ServerInfoHeader">http://www.w3.org/2001/XMLSchema"><soap:Header><ServerInfoHeader
> xmlns="<ReportServerVersionNumber>Microsoft">http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><ReportServerVersionNumber>Microsoft
> SQL Server Reporting Services Version
> 8.00.1038.00</ReportServerVersionNumber><ReportServerEdition>Enterprise</ReportServerEdition></ServerInfoHeader><SessionHeader
> xmlns="<SessionId>glbci3rl3utt1iqmon253t45</SessionId><IsNewExecution>true</IsNewExecution><ExecutionDateTime>2007-02-11T19:32:50</ExecutionDateTime><ExpirationDateTime>2007-02-11T19:32:50</ExpirationDateTime></SessionHeader></soap:Header><soap:Body><RenderResponse">http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><SessionId>glbci3rl3utt1iqmon253t45</SessionId><IsNewExecution>true</IsNewExecution><ExecutionDateTime>2007-02-11T19:32:50</ExecutionDateTime><ExpirationDateTime>2007-02-11T19:32:50</ExpirationDateTime></SessionHeader></soap:Header><soap:Body><RenderResponse
> xmlns="<Result>">http://schemas.microsoft.com/sqlserver/2003/12/reporting/reportingservices"><Result>
> Base 64 Encoded response removed to shorten post
> </Result><Encoding>Unicode
> (UTF-8)</Encoding><MimeType>text/html</MimeType><ParametersUsed><ParameterValue><Name>p1</Name><Value>2/11/2007
> 7:32:50 PM</Value></ParameterValue></ParametersUsed><StreamIds
> /></RenderResponse></soap:Body></soap:Envelope>
>