When using sp_send_dbmail like this :
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MY_PROFILE'
,#recipients = 'MY_EMAIL_ADDRESS'
,#query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM TABLE1'
,#subject = 'MY_SUBJECT'
,#body = 'This is a test'
,#attach_query_result_as_file = 1
,#query_attachment_filename = 'file.csv'
,#query_result_separator = ' '
;
The attached files is always empty. I tried my query outside of sp_send_dbmail and it works perfectly. I also tried to replace my query by SELECT 1 and then my attached file has data.
What could cause my query to return no data?
I found how to solve it. I had to specify the database before the table name. Something more like :
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'MY_PROFILE'
,#recipients = 'MY_EMAIL_ADDRESS'
,#query = 'SELECT TOP 50 FIELD1, FIELD2, FIELD3 FROM DATABADE.dbo.TABLE1'
,#subject = 'MY_SUBJECT'
,#body = 'This is a test'
,#attach_query_result_as_file = 1
,#query_attachment_filename = 'file.csv'
,#query_result_separator = ' '
;
Related
I need to write a query based on the following conditions:
Within one table, the following condition must be met:
table name: DLS_RBM_RBI
columns within the table
RBM_3_CD must be blank
If RBM_3_CD is blank, then RBM_2_CD must be 1
If RBM_3_CD is blank and RBM_2_CD is blank, then RBM_1_CD must be 1
How would these conditions be coded in a SQL statement? In an IF within a where clause or case in a select?
I tried using a where clause with these conditions.
AND (D.RBM_3_CD = '')
OR (D.RBM_3_CD = '' AND D.RBM_2_CD = '1')
OR (D.RBM_3_CD = '' AND RBM_2_CD = '' AND RBM_1_CD = '1')
The first statement
AND (D.RBM_3_CD = ' ')
negates the need for the second and third. There's no way for the first statement to false while the second or third are true.
try this instead
where D.RBM_3_CD = ' '
and ( (D.RBM_3_CD = '' AND D.RBM_2_CD = '1')
or (D.RBM_3_CD = '' AND RBM_2_CD = '' AND RBM_1_CD = '1')
)
I need to write a MERGE statement to insert data WHEN NOT MATCHED condition, WHEN MATCHED I'd like the query to do nothing but I've got to include this condition because I badly need to catch the source data from both conditions into my output table.
Here's my code:
MERGE dm_data_bps.dbo.akcja AS target
USING (
SELECT *
FROM #CEIDG
WHERE isnull(sp_id, '') <> ''
) AS source
ON target.ak_id = source.ceidg_ak_id
WHEN NOT MATCHED
THEN
INSERT (
ak_akt_id
,ak_sp_id
,ak_kolejnosc
,ak_interwal
,ak_zakonczono
,ak_pr_id
,ak_publiczna
)
VALUES (
1246
,sp_id
,0
,0
,getdate()
,5
,1
)
WHEN MATCHED
THEN
UPDATE
<DO NOTHING>
OUTPUT inserted.ak_id
,source.Firma
,source.AdresPocztyElektronicznej
,source.AdresStronyInternetowej
,source.IdentyfikatorWpisu
,source.DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,source.DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,source.DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,source.DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,source.DataWykresleniaWpisuZRejestru
,source.MalzenskaWspolnoscMajatkowa
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,source.SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,source.Zakazy
,source.InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,source.Sukcesja
,source.AdresGlownegoMiejscaWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,source.AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,source.AdresDoDoreczen
,source.STATUS
INTO #ceidg_ak_id;
How can I accomplish my goal?
I'm not sure I'd bother with all of the overhead that comes with a MERGE statement. See Use Caution with SQL Server's MERGE Statement.
You can get everything you need with an explicit transaction.
BEGIN TRANSACTION;
UPDATE
target
SET
ak_akt_id = 1246
,ak_sp_id = sp_id
,ak_kolejnosc = 0
,ak_interwal = 0
,ak_zakonczono = GETDATE()
,ak_pr_id = 5
,ak_publiczna = 1
FROM
dm_data_bps.dbo.akcja AS target
JOIN
(SELECT * FROM #CEIDG WHERE sp_id <> '') AS source
ON
target.ak_id = source.ceidg_ak_id;
SELECT
Firma
,AdresPocztyElektronicznej
,AdresStronyInternetowej
,IdentyfikatorWpisu
,DataRozpoczeciaWykonywaniaDzialalnosciGospodarczej
,DataZawieszeniaWykonywaniaDzialalnosciGospodarczej
,DataWznowieniaWykonywaniaDzialalnosciGospodarczej
,DataZaprzestaniaWykonywaniaDzialalnosciGospodarczej
,DataWykresleniaWpisuZRejestru
,MalzenskaWspolnoscMajatkowa
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaNIP
,SpolkiCywilneKtorychWspolnikiemJestPrzedsiebiorcaREGON
,Zakazy
,InformacjeDotyczaceUpadlosciPostepowaniaNaprawczego
,Sukcesja
,AdresGlownegoMiejscaWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci
,AdresyDodatkowychMiejscWykonywaniaDzialalnosci2
,AdresDoDoreczen
,STATUS
INTO
#ceidg_ak_id
FROM
#CEIDG
WHERE
sp_id <> '';
COMMIT TRANSACTION;
I Have sample CSV file which contains 10 records.
So I want to upload the CSV file Thru stored procedure.
Is it possible to do that way. This is my stored function.
FOR i IN 1..v_cnt LOOP
SELECT idx_date,file_path INTO v_idx_date,v_file_path FROM cloud10k.temp_idx_dates
WHERE is_updated IS FALSE LIMIT 1;
COPY cloud10k.temp_master_idx_new(header_section) FROM v_file_path;
DELETE FROM cloud10k.temp_master_idx_new WHERE header_section NOT ILIKE '%.txt%';
UPDATE cloud10k.temp_master_idx_new SET CIK = split_part( header_section,'|',1),
company_name = split_part( header_section,'|',2),
form_type = split_part( header_section,'|',3),
date_filed = split_part( header_section,'|',4)::DATE,
accession_number = replace(split_part(split_part( header_section,'|',5),'/',4),'.txt',''),
file_path = to_char(SUBSTRING(SPLIT_PART(v_file_path,'master.',2) FROM 1 FOR 8)::DATE,'YYYY')
||'/'||to_char(SUBSTRING(SPLIT_PART(v_file_path,'master.',2) FROM 1 FOR 8)::DATE,'MM')
||'/'||to_char(SUBSTRING(SPLIT_PART(v_file_path,'master.',2) FROM 1 FOR 8)::DATE,'DD')
||'/'||CONCAT_WS('.','master',SPLIT_PART(v_file_path,'master.',2) )
WHERE header_section ILIKE '%.txt%';
END LOOP;
But its not executing. Can someone suggest me how to do that..
Tanks,
Ramesh
Hi i am new to oracle_sqldeveloper can you please give me the answer how to know the table structure and relationships of a database.
You can try
DESC <table_name>
Try this:
select table_name, column_name, data_type
from all_tab_columns
where table_name = <TABLE_NAME_HERE>
and owner = '<YOUR_USER_HERE_IN_CAPITAL_LETTERS>'
If you have comments on your table then to get columns' comments:
select tc.table_name, tc.column_name, tc.data_type, cc.comments
from all_col_comments cc, all_tab_columns tc
where tc.table_name = <TABLE_NAME_HERE>
and tc.owner = <OWNER_OF_TABLE_HERE>
and tc.table_name = cc.table_name
and tc.column_name = cc.column_name
and tc.owner = cc.owner
If you are logged in under owner of the table you can write this:
select table_name, column_name, data_type
from user_tab_columns
where table_name = <TABLE_NAME_HERE>
or to get columns with comments
select tc.table_name, tc.column_name, tc.data_type, cc.comments
from user_col_comments cc, user_tab_columns tc
where tc.table_name = '<TABLE_NAME_HERE>'
and tc.owner = '<YOUR_USER_HERE_IN_CAPITAL_LETTERS>'
and tc.table_name = cc.table_name
and tc.column_name = cc.column_name
To get relationships between tables user this query:
select uc1.table_name
, uc1.constraint_name
, cc1.column_name
, uc2.table_name r_table_name
, uc2.constraint_name r_constraint_name
, cc2.column_name r_column_name
from all_constraints uc1
, all_constraints uc2
, all_cons_columns cc1
, all_cons_columns cc2
where 1 = 1
and uc2.constraint_type = 'R'
and uc1.constraint_name = uc2.r_constraint_name
and cc1.table_name = uc1.table_name
and cc1.constraint_name = uc1.constraint_name
and cc2.table_name = uc1.table_name
and cc2.constraint_name = uc1.constraint_name
and uc1.owner = '<YOUR_USER_HERE_IN_CAPITAL_LETTERS>'
and uc2.owner = uc1.owner
and cc1.owner = uc1.owner
and cc2.owner = uc1.owner
order by 1
/
Columns with the "R_" prefix mean that they are foreign data (they represent foreign keys). As you can see, I used the tables with the "ALL_" prefix, to use similar tables with the "USER_" prefix, get rid of the "OWNER" section.
To know more about oracle data dictionary read this
1) type your table name.
2) right click on table name & click Open Declaration.
I am trying to find all records in my #TempTable that are not in the staging table.
Its important to note that the comparison needs to take place over 16 fields.
I have tried several combinations and nothing seems to work.
SELECT CustomerAccountNo FROM #TempTable
WHERE NOT EXISTS
(SELECT e.[CustomerAccountNo] ,
e.[MeterNo] ,
e.[CustomerName1] ,
e.[ServiceAddress1] ,
e.[ServiceAddress2] ,
e.[ServiceCity] ,
e.[ServiceState] ,
e.[ServiceZip] ,
e.[BillingAddress1] ,
e.[BillingAddress2] ,
e.[BillingAddress3] ,
e.[BillingCity] ,
e.[BillingState] ,
e.[BillingZip] ,
e.[BillingZip4] ,
e.[PrimaryPhoneNumber] FROM #TempTable e
JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)
Instead of a JOIN, try using Except.
SELECT CustomerAccountNo, MeterNo -- and so on
FROM #TempTable
EXCEPT
SELECT CustomerAccountNo, MeterNo -- and so on
FROM Staging
Just do a join and look for null. Like this
SELECT e.*
FROM #TempTable e
LEFT JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumb
WHERE s.CustomerAccountNo is null
You should provide more detailed circumstance to get correct answer.
Clearly, you don't have any connection between your FROM clause and WHERE clause so the query will return all.
Try this one:
SELECT CustomerAccountNo FROM #TempTable t
WHERE NOT EXISTS
(SELECT 1 FROM dbo.Staging s WHERE
t.CustomerAccountNo = s.CustomerAccountNo AND
t.MeterNo = s.MeterNo AND
t.CustomerName1 = s.CustomerName1 AND
t.ServiceAddress1 = s.ServiceAddress1 AND
t.ServiceAddress2 = s.ServiceAddress2 AND
t.ServiceCity = s.ServiceCity AND
t.ServiceState = s.ServiceState AND
t.ServiceZip = s.ServiceZip AND
t.BillingAddress1 = s.BillingAddress1 AND
t.BillingAddress2 = s.BillingAddress2 AND
t.BillingAddress3 = s.BillingAddress3 AND
t.BillingCity = s.BillingCity AND
t.BillingState = s.BillingState AND
t.BillingZip = s.BillingZip AND
t.BillingZip4 = s.BillingZip4 AND
t.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)