Showing posts with label written. Show all posts
Showing posts with label written. Show all posts

Thursday, March 29, 2012

Get all records

Hi I have a table route with

routeid name

1 a

2 b

3 c

4 d

5 e

..

10 j

exExposure

caseid routeid

200 1

300 2

..

Now I have written a query which will join some other tables to get the results

select

er.exRouteID as RouteID,er.[Name] as route,

'<=5 Years'=SUM(CASE WHEN((pp.paAgeUnitId=1) THEN 1 ELSE 0 END ),

'6-12 Years'=SUM(CASE WHEN((pp.paAgeUnitId=2) THEN 1 ELSE 0 END ),

from cacase c

left join exExposure e on c.caCaseID=e.caCaseID

Left Join Route er on er.RouteID=e.RouteID

where --conditions

When i run this query I am getting only the routes which have the values exist in exExposure table.If for some routes like i, j I don't have corresponding casesids.But i need to get all the routes which exist in Route table even they don't have caseids.

For this i am trying like this:

select

er.exRouteID as RouteID,er.[Name] as route,

'<=5 Years'=SUM(CASE WHEN((pp.paAgeUnitId=1) THEN 1 ELSE 0 END ),

'6-12 Years'=SUM(CASE WHEN((pp.paAgeUnitId=2) THEN 1 ELSE 0 END ),

from cacase c

left join exExposure e on c.caCaseID=e.caCaseID

Left Join Route er on er.RouteID=e.RouteID

where --conditions

Union

Select er.RouteID,er.Name,0,0

From Route r

where r.routeID Not IN (Selelct RouteID From above Selelct query with the same where condtions and joins)

Any other better way for getting the unmatched routes other than this.

Thanks in advance.

I'm not sure what your question is, but I'll take a guess.

If you want the original query to contain all Routes (Route table), then name the Route table as the FIRST table in the series of joins, OR, in the current query, use a RIGHT JOIN with Routes. Either option 'should' give you what you seek.

|||

You can get your desired result using the following query..

select

er.exRouteID as RouteID,er.[Name] as route,

'<=5 Years'=SUM(CASE WHEN((pp.paAgeUnitId=1) THEN 1 ELSE 0 END ),

'6-12 Years'=SUM(CASE WHEN((pp.paAgeUnitId=2) THEN 1 ELSE 0 END ),

from cacase c

Left Join exExposure e on c.caCaseID=e.caCaseID And {all Your exExposure table based conditions}

Left Join Route er on er.RouteID=e.RouteID And {all your Route table based conditions}

where {other conditions}

|||

Hi,

I placed all the ExExposure table condion after the exExposure table join this is for Route table also. But now also it is giving Routes which have the CaseID s only. Any other way.

Thanks in advance.

|||

The following query will be help you...

Code Snippet

select

er.exRouteID as RouteID,er.[Name] as route,

'<=5 Years'=SUM(CASE WHEN((pp.paAgeUnitId=1) THEN 1 ELSE 0 END ),

'6-12 Years'=SUM(CASE WHEN((pp.paAgeUnitId=2) THEN 1 ELSE 0 END ),

from Route er

Left Join exExposure e on er.RouteID=e.RouteID

Left Join cacase c on c.caCaseID=e.caCaseID

where other condition

Friday, February 24, 2012

Generate Flat File via Stored Procedure

I have a need to do the following:

Generate a Stored Procedure and have the output written in a csv format.

I have everything I need to capture the data via stored procedure, but I am lost on a way to 'INSERT' the data values into a csv file.

This stored procedure will be triggered by another application.

Could someone please help.

thanks

there are many ways to do that

1. in sql server 200 you can use a dts to out put the result

2. in sql server 2005 you can use SSIS

3. you can use the openrowset function

4. you cann configure a linked server and output the result.

|||

This might help

create procedure usp_runbcppkg

as

declare @.as_bcp varchar(255)

declare @.as_query nvarchar(255)

declare @.as_select varchar(255)

declare @.as_appcode varchar(20)

set @.as_select = 'bcp "select appcode, servername, region, usage_ctr, st_date, ed_date from pubs..temp"'

set @.as_query = 'master.dbo.xp_cmdshell ' + @.as_bcp

begin

--cursor to get the list of state

declare cur_appcode cursor for

select distinct state from authors

order by 1

open cur_appcode

fetch next from cur_appcode into @.as_appcode

while @.@.fetch_status = 0

begin

Truncate & Insert the date for the state in Temp Table

truncate table temp

insert into temp (au_id, au_lname, phone, address, city, state, zip)

select au_id, au_lname, phone, address, city, state, zip

from pubs..authors where state = @.as_appcode

--<Servername> to be entered

--<Password> to be entered

set @.as_bcp = @.as_select + ' queryout c:\temp\' + @.as_appcode + '.xls -S<Servername> -Usa -P<Password> -w'

exec master..xp_cmdshell @.as_bcp

fetch next from cur_appcode into @.as_appcode

end

close cur_appcode

deallocate cur_appcode

end|||

An OPENROWSET example which emails the results.

@.reportxls must exist on the server (an empty xls flle with column headers only).

Openrowset will append results to the file. If this is what you want then remove the copy, rename and delete parts of the code.

CREATE PROCEDURE sp_Test
(@.selectcmd as varchar(400), @.email_list as varchar(300), @.reportxls as varchar(40))

as

declare @.result as int, @.attach as varchar(140), @.renamecmd as varchar(200), @.deletecmd as varchar(200), @.copycmd as varchar(200), @.insertcmd as varchar(200), @.sql as varchar(600)
set @.attach = 'd:\reports\' + @.reportxls
set @.copycmd = 'copy d:\reports\' + @.reportxls + ' d:\reports\' + @.reportxls + '#'
set @.renamecmd = 'ren d:\reports\' + @.reportxls + '# ' + @.reportxls
set @.deletecmd = 'del d:\reports\' + @.reportxls
set @.insertcmd = 'INSERT INTO OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',''Excel 8.0;Database=d:\reports\' + @.reportxls + ';'', ''SELECT * FROM [Sheet1$]'')'
set @.sql = @.insertcmd + @.selectcmd


EXEC @.result = master.dbo.xp_cmdshell @.copycmd
IF (@.result = 0)
BEGIN
EXEC (@.sql)
EXEC master.dbo.xp_sendmail @.recipients = @.email_list,
@.message = 'Test Body Message',
@.subject = 'Report Request',
@.attachments = @.attach
EXEC master.dbo.xp_cmdshell @.deletecmd
EXEC master.dbo.xp_cmdshell @.renamecmd
END

GO