set ansi_warnings off not working - sql

Why do I still receiving in the emails: Warning: Null value is eliminated by an aggregate or other SET operation. If I have set the ansi_warnings off
set ansi_warnings off
set #subj = 'xxx'
set #msg = 'xxx' + char(13) + char(10)
set #q = 'select LSRNbr, trh.ShipperId, InvoiceNbr, Qty, count(LotSerNbr) as QtyRcvd
from tblRIM2_Header trh (nolock)
left outer join tblRIM2_SerialsFromFile trs (nolock) on trh.ShipperId = trs.ShipperId
where isnull(Completed,0) = 0
group by trh.ShipperId, LSRNbr, InvoiceNbr, Qty'
exec master.dbo.xp_sendmail
#recipients = 'xxx',
#subject = #subj ,
#query = #q,
#message = #msg,
#attach_results = FALSE,
#dbuse = 'GlobalQCS'
set ansi_warnings on

You have to put set ansi_warnings off inside #q:
set #q = 'set ansi_warnings off select LSRNbr, trh.ShipperId, ...`

Related

SSIS Error: Unable to retrieve destination column descriptions from the parameters of the SQL command

I am getting an error :
[Load updated records to Table_vwAs_FixedIdentifier [85]] Error: Unable to retrieve destination column descriptions from the parameters of the SQL command.
when I execute the SSIS task in the image. It fails on the lower left "Load updated records to Table_vwAs.." oleDb update task. It has the following sql statement and also has a trigger also shown below. I have not been able to identify the cause of this issue till now. When I disabled the trigger, the task ran successfully. Any help will be eye-opening.
UPDATE
[dbo].[Sec]
SET
[SecID]=?
,[SECURITY_NAME]=?
,[SECURITY_SECTYPE_ID]=?
,[SECURITY_EXCHANGE_ID]=?
WHERE
[MasterID]=?
Trigger code is as follows:
alter trigger [dbo].[TrTable_Fixed_AFTERUPDATE] on [dbo].[Sec]
after insert ,update
as
set NOCOUNT on
declare #DBMailDistributionListId varchar(50)
declare #ProfileName varchar(max)
declare #Recipients varchar(max)
declare #CopyRecipients varchar(max)
declare #BlindCopyRecipients varchar(max)
set #DBMailDistributionListId = 'Crossadyne1'
set #ProfileName = SharedCode.dbo.FnGetDBMailDistributionListProfileName(#DBMailDistributionListId)
set #Recipients = SharedCode.dbo.FnGetDBMailDistributionListRecipients(#DBMailDistributionListId)
set #CopyRecipients = SharedCode.dbo.FnGetDBMailDistributionListCopyRecipients(#DBMailDistributionListId)
set #BlindCopyRecipients = SharedCode.dbo.FnGetDBMailDistributionListBlindCopyRecipients(#DBMailDistributionListId)
declare #Subject varchar(max)
declare #Body varchar(max)
set #Body = ''
select distinct #Body = #Body + 'Master Sec ID: ' + convert(nvarchar(50) ,i.MasterId) + char(13) + char(10)
+ 'New Legal Entity ID: ' + convert(nvarchar(50) ,i.[INTERNAL_SECURITY_LEGALENTITY_ID])
from INSERTED i
inner join DELETED d
on i.MasterId = d.MasterId
where i.[INTERNAL_SECURITY_LEGALENTITY_ID] <> d.[INTERNAL_SECURITY_LEGALENTITY_ID]
and i.Security_Change_Date = i.LegalEntityChangeDate
and i.LegalEntityChangedBy not in (2 ,5)
if (#Body <> '')
begin
set #Subject = 'WARNING: Crossadyne Security Legal Entity changed'
exec msdb.dbo.sp_send_dbmail
#profile_name = #ProfileName
, #recipients = #Recipients
, #copy_recipients = #CopyRecipients
, #blind_copy_recipients = #BlindCopyRecipients
, #subject = #Subject
, #body = #Body
end
set #DBMailDistributionListId = 'test'
set #ProfileName = SharedCode.dbo.FnGetDBMailDistributionListProfileName(#DBMailDistributionListId)
set #Recipients = SharedCode.dbo.FnGetDBMailDistributionListRecipients(#DBMailDistributionListId)
set #CopyRecipients = SharedCode.dbo.FnGetDBMailDistributionListCopyRecipients(#DBMailDistributionListId)
set #BlindCopyRecipients = SharedCode.dbo.FnGetDBMailDistributionListBlindCopyRecipients(#DBMailDistributionListId)
set #Body = ''
select distinct #Body = #Body + 'Master ID: ' + convert(nvarchar(50) ,i.MasterId) + char(13) + char(10)
+ 'Previous Legal Entity ID: ' + convert(nvarchar(50) ,d.[INTERNAL_SECURITY_LEGALENTITY_ID])
from INSERTED i
inner join DELETED d
on i.MasterId = d.MasterId
where i.[INTERNAL_SECURITY_ID] <> d.[INTERNAL_SECURITY_LEGALENTITY_ID]
if (#Body <> '')
begin
set #Subject = 'WARNING: Crossadyne Security Legal Entity changed'
exec msdb.dbo.sp_send_dbmail
#profile_name = #ProfileName
, #recipients = #Recipients
, #copy_recipients = #CopyRecipients
, #blind_copy_recipients = #BlindCopyRecipients
, #subject = #Subject
, #body = #Body
end
set NOCOUNT off
The issue was being caused due to
exec msdb.dbo.sp_send_dbmail
in the trigger code, because it is not configured on the QA server. Resolved after I commented those lines.

SSIS package for replacing the stored procedure with cursor and different servers with join clauses

I am trying to create a ssis package for replacing the below stored procedures. It has join clauses, cursors and are using different servers. So I have been trying to figure out the solution for it. If anyone have any concepts so that I get rid of the linked server connection issue. Thank you in advance. Any kind of useful steps and hints. I really appreciate it.
ALTER PROCEDURE [dbo].[AUDIT_FILES] AS
SET NOCOUNT ON
DECLARE
#cmd nvarchar(4000)
, #working_dir nvarchar(240)
, #file_name nvarchar(240)
, #file_list nvarchar(2000)
, #table_name sysname
, #audit_count int
, #email_to nvarchar(MAX)
, #email_subject nvarchar(240)
, #email_body nvarchar(MAX)
, #email_attach nvarchar(240)
, #top_recs int
, #sql nvarchar(MAX);
BEGIN TRY
SET #working_dir = 'k:\sql\data\WWE037\audit';
SET #cmd = 'del /q "' + #working_dir + '*.*"';
EXEC sys.xp_cmdshell #cmd, NO_OUTPUT;
IF CURSOR_STATUS('global','c_audit_tables') > -1 DEALLOCATE c_audit_tables;
DECLARE c_audit_tables CURSOR
FOR
SELECT
a.TABLE_NAME
, a.TOP_NUM_REC
FROM
[CSVP0007\WWE037].Audit.INFORMATION_SCHEMA.TABLES i
INNER JOIN WebPM_Copy.dbo.AUD_TABLE_LIST a ON
i.TABLE_NAME = a.TABLE_NAME
INNER JOIN [CSVP0007\WWE037].Audit.sys.tables t on
a.TABLE_NAME = t.name
INNER JOIN [CSVP0007\WWE037].Audit.sys.partitions p ON
p.object_id = t.object_id
GROUP BY a.TABLE_NAME, a.TOP_NUM_REC
HAVING SUM(rows) > 1
ORDER BY 1;
OPEN c_audit_tables;
FETCH c_audit_tables INTO #table_name, #top_recs;
WHILE ##fetch_status = 0
BEGIN
SET #file_name = #table_name + '.txt';
SET #audit_count = COALESCE(#audit_count,0) + 1;
SET #cmd = 'SELECT TOP {Recs} * INTO dbo.TMP_AUDIT_EXPORT FROM
[CSVP0007\WWE037].Audit.dbo.{TableName}';
SET #cmd = REPLACE(#cmd,'{Recs}',#top_recs);
SET #cmd = REPLACE(#cmd,'{TableName}',#table_name);
IF OBJECT_ID('TMP_AUDIT_EXPORT') IS NOT NULL
DROP TABLE TMP_AUDIT_EXPORT;
EXEC sys.sp_executesql #cmd;
EXEC util.dbo.usp_export_table
#db_name = 'wb_Copy'
, #schema_name = 'dbo'
, #table_name = 'TMP_AUDIT_EXPORT'
, #output_dir = #working_dir
, #output_file = #file_name
, #delimiter = '|'
, #use_native_mode = 0
, #location_code = 'DEV_Wb_COPY';
FETCH c_audit_tables INTO #table_name, #top_recs;
END
CLOSE c_audit_tables;
DEALLOCATE c_audit_tables;
SET #file_name = 'Audit_FCL_' + CONVERT(CHAR(8),GETDATE(),112) + '.zip';
SET #file_list = #working_dir + '\*.txt';
EXEC BIA_Utils_User.dbo.usp_zip_files
#zip_dir = #working_dir
, #zip_file = #file_name
, #file_list = #file_list
, #zip_password = NULL;
SET #email_subject = 'Wb Daily Audit Testing: ' + CONVERT(VARCHAR,
GETDATE(), 101)
SET #email_attach = #working_dir + '\' + #file_name;
PRINT #email_attach
SET #email_body = '*** Number of Audit File(s) To Be Reviewed: ' +
CONVERT(NVARCHAR,#audit_count);
EXEC Utils_User.dbo.usp_send_bia_mail
#profile_name = 'WB'
, #subject = #email_subject
, #body = #email_body
, #body_format = 'TEXT'
, #importance = 'NORMAL'
, #file_attachments = #email_attach
, #app_code = 'wb_app'
, #bnotify_app_group = 'Audit_Email';
END TRY

SQL Server dynamic parameters join stored procedure

This my stored procedure and parameters but I need to do dynamic search
SELECT
dbo.Invoices.*,
dbo.Vessels.Name AS VesselName,
dbo.Companies.Name AS CompanyName,
dbo.InvoiceTypes.Name AS InvoiceTypeName,
dbo.InvoiceItems.Name AS InvoiceItemName,
dbo.InvoiceItems.InvoiceItemID
FROM
dbo.Invoices
LEFT JOIN dbo.Vessels ON
dbo.Invoices.VesselID = dbo.Vessels.VesselID
INNER JOIN dbo.Companies ON
dbo.Invoices.CompanyID = dbo.Companies.CompanyID
LEFT JOIN dbo.InvoiceTypes ON
dbo.Invoices.InvoiceTypeID = dbo.InvoiceTypes.InvoiceTypeID
LEFT JOIN dbo.InvoiceVsInvoiceItems ON
dbo.Invoices.InvoiceID = dbo.InvoiceVsInvoiceItems.InvoiceID
LEFT JOIN dbo.InvoiceItems ON
dbo.InvoiceVsInvoiceItems.InvoiceItemID = dbo.InvoiceItems.InvoiceItemID
PARAMETERS
#InvoiceItemID int,
#InvoiceTypeID int,
#VesselID int,
#PaidByID int,
#InvoiceNo NVarchar(50),
#CompanyID int,
#chkSearchInvoiceDate bit,
#chkSearchIsDueDate bit,
#chkSearchIsPaid bit,
#chkSearchReceived bit,
#chkSearchAmount bit,
#chkSearchInvoiceType bit,
#InvoiceFromDate DateTime,
#InvoiceToDate DateTime,
#FromDueDate DateTime,
#ToDueDate DateTime,
#FromAmount decimal(18,4),
#ToAmount decimal(18,4)
But I tried it what do I do when there are multiple where? I could not :(
Thank you
Declare #SQLQuery AS NVarchar(4000)
Declare #ParamDefinition AS NVarchar(2000)
Set #SQLQuery = 'SELECT dbo.Invoices.*, dbo.Vessels.Name AS VesselName, dbo.Companies.Name AS CompanyName, dbo.InvoiceTypes.Name AS InvoiceTypeName,
dbo.InvoiceItems.Name AS InvoiceItemName, dbo.InvoiceItems.InvoiceItemID'
If #InvoiceItemID Is Not Null
Set #SQLQuery = #SQLQuery + 'FROM dbo.Invoices LEFT JOIN dbo.InvoiceItems ON dbo.InvoiceVsInvoiceItems.InvoiceItemID = dbo.InvoiceItems.InvoiceItemID WHERE dbo.InvoiceVsInvoiceItems.InvoiceItemID = #InvoiceItemID'
If #VesselID Is Not Null
Set #SQLQuery = #SQLQuery + 'LEFT JOIN dbo.Vessels ON dbo.Vessels.VesselID = dbo.Invoices.VesselID WHERE dbo.Invoices.VesselID = #VesselID'
If #InvoiceNo Is Not Null
Set #SQLQuery = #SQLQuery + 'WHERE dbo.Invoices.InvoiceNo = #InvoiceNo'
If #CompanyID Is Not Null
Set #SQLQuery = #SQLQuery + 'WHERE dbo.Invoices.CompanyID = #CompanyID'
If #chkSearchInvoiceDate > 0
Set #SQLQuery = #SQLQuery + 'WHERE Between #InvoiceFromDate and #InvoiceToDate'
If #chkSearchIsDueDate > 0
Set #SQLQuery = #SQLQuery + 'WHERE Between #FromDueDate and #ToDueDate'
If #chkSearchIsPaid > 0
Set #SQLQuery = #SQLQuery + 'WHERE dbo.Invoices.PaidBy = #PaidBy'
If #chkSearchReceived > 0
Set #SQLQuery = #SQLQuery + 'WHERE dbo.Invoices.InvoiceNo = #InvoiceNo'
If you want to be able to use multiple parameters in the where clause at the same time you can add them like this:
Declare #SQLQuery AS NVarchar(4000)
Declare #ParamDefinition AS NVarchar(2000)
Set #SQLQuery = '
SELECT
dbo.Invoices.*,
dbo.Vessels.Name AS VesselName,
dbo.Companies.Name AS CompanyName,
dbo.InvoiceTypes.Name AS InvoiceTypeName,
dbo.InvoiceItems.Name AS InvoiceItemName,
dbo.InvoiceItems.InvoiceItemID
FROM dbo.Invoices
LEFT JOIN dbo.InvoiceItems ON dbo.InvoiceVsInvoiceItems.InvoiceItemID = dbo.InvoiceItems.InvoiceItemID
LEFT JOIN dbo.Vessels ON dbo.Vessels.VesselID = dbo.Invoices.VesselID
WHERE 1=1 '
If #InvoiceItemID Is Not Null
Set #SQLQuery = #SQLQuery + ' AND dbo.InvoiceVsInvoiceItems.InvoiceItemID = #InvoiceItemID'
If #VesselID Is Not Null
Set #SQLQuery = #SQLQuery + ' AND dbo.Invoices.VesselID = #VesselID'
If #InvoiceNo Is Not Null
Set #SQLQuery = #SQLQuery + ' AND dbo.Invoices.InvoiceNo = #InvoiceNo'
If #CompanyID Is Not Null
Set #SQLQuery = #SQLQuery + ' AND dbo.Invoices.CompanyID = #CompanyID'
If #chkSearchInvoiceDate > 0
Set #SQLQuery = #SQLQuery + ' AND InvoiceDate Between #InvoiceFromDate and #InvoiceTODate'
If #chkSearchIsDueDate > 0
Set #SQLQuery = #SQLQuery + ' AND IsDueDate Between #FromDueDate and #ToDueDate'
If #chkSearchIsPaid > 0
Set #SQLQuery = #SQLQuery + ' AND dbo.Invoices.PaidBy = #PaidByID'
If #chkSearchReceived > 0
Set #SQLQuery = #SQLQuery + ' AND dbo.Invoices.InvoiceNo = #InvoiceNo'

Invalid Column Name '#FileName-- value'

I am getting 'Invalid Column Name '#FileName-- value'' ERROR WHILE executing below stored procedure. I tried to find the root cause of the issue with no luck...Please suggest me where i am wrong.
ALTER PROCEDURE [dbo].[usp_ICLExtract_GetFile]
#FileName Varchar(50),
#Image_Path Varchar(50) Output,
#FIleNameList varchar(4096) OUTPUT
AS
BEGIN
SET NOCOUNT ON
DECLARE #strProcName varchar(255)
SET #strProcName = 'usp_ICLExtract_GetFile'
DECLARE #strSQL1 varchar(1024), #strSQL2 varchar(1024)
DECLARE #strFileName Varchar(50)
DECLARE #intErrorReturn int
SET #intErrorReturn = 0
SET #FileNameList = ''
SET #Image_Path = ''
SET #strSQL1 = N'SELECT tbl_ICLExtCashLetter.Image_Path from tbl_ICLExtCashLetter INNER JOIN tbl_ICLExtFile on [tbl_ICLExtCashLetter].ICLExtFileUID = [tbl_ICLExtFile].ICLExtFileUID where tbl_ICLExtFile.FileName = ' + #FileName --This line cause error
/*==============================================================================
* Run the query'
*==============================================================================*/
SET #strSQL2 = 'DECLARE curCategory INSENSITIVE SCROLL CURSOR FOR ' + #strSQL1
EXEC(#strSQL2)
SELECT #intErrorReturn = ##ERROR
IF (#intErrorReturn <> 0) GOTO usp_ICLExtract_GetFile_Error
OPEN curCategory
SELECT #intErrorReturn = ##ERROR
IF (#intErrorReturn <> 0) GOTO usp_ICLExtract_GetFile_Error
FETCH NEXT FROM curCategory INTO #strFileName
WHILE (##FETCH_STATUS <> -1)
BEGIN
SET #FileNameList = #FileNameList + #strFileName + ';'
SET #Image_Path = #Image_Path + 1
FETCH NEXT FROM curCategory INTO #strFileName
END
CLOSE curCategory
DEALLOCATE curCategory
RETURN(0)
usp_ICLExtract_GetFile_Error:
RETURN(#intErrorReturn)
END
replace line with this
SET #strSQL1 = N'SELECT tbl_ICLExtCashLetter.Image_Path from tbl_ICLExtCashLetter INNER JOIN tbl_ICLExtFile on [tbl_ICLExtCashLetter].ICLExtFileUID = [tbl_ICLExtFile].ICLExtFileUID where tbl_ICLExtFile.FileName = ''' + #FileName +'''
Change your query to this:
N'SELECT tbl_ICLExtCashLetter.Image_Path from tbl_ICLExtCashLetter
INNER JOIN tbl_ICLExtFile on [tbl_ICLExtCashLetter].ICLExtFileUID =
[tbl_ICLExtFile].ICLExtFileUID where tbl_ICLExtFile.FileName = ''' + #FileName + ''''
you have to pass value between apostrophes '.

SQL Server stored procedure

I want it to count then if #intcount > 0 it should show data or else no data found, but when I execute it gives me 'no data found' regardless, what am I doing wrong?
#FiscalYear int,
#SchoolID int,
#Status int
AS
BEGIN
SET NOCOUNT ON;
declare #sqlstr varchar(2000)
declare #intCount int
set #intCount = 0
set #sqlstr = 'Select #intCount = Count(*)
From PrivateSchool left outer join Attachment on Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID
inner join FiscalYearPrivateSchool fp ON fp.PrivateSchoolID = PrivateSchool.PrivateSchoolID
Where (FiscalYear = '+convert(varchar, #FiscalYear)+') AND (PrivateSchool.IsActive = 1)'
IF (#SchoolID != -1)
SET #sqlstr = #sqlstr + ' AND SchoolID ='+ convert(varchar, #SchoolID)
IF (#Status = -1)
SET #sqlstr = #sqlstr + ' AND PrivateSchool.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else IF (#Status = 1)
SET #sqlstr = #sqlstr + ' AND Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else
SET #sqlstr = #sqlstr + ' AND Attachment.PrivateSchoolID is Null'
If (#intCount > 0)
BEGIN
set #sqlstr= 'Select SchoolName as School,
(Case when Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID THEN ''Uploaded''
ELSE ''Not Uploaded'' END) AS Status,
COUNT(Attachment.PrivateSchoolID) AS [Count]
From PrivateSchool left outer join Attachment on Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID
inner join FiscalYearPrivateSchool fp ON fp.PrivateSchoolID = PrivateSchool.PrivateSchoolID
Where (FiscalYear = '+convert(varchar, #FiscalYear)+') AND (PrivateSchool.IsActive = 1)'
IF (#SchoolID != -1)
SET #sqlstr = #sqlstr + ' AND SchoolID ='+ convert(varchar, #SchoolID)
IF (#Status = -1)
SET #sqlstr = #sqlstr + ' AND PrivateSchool.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else IF (#Status = 1)
SET #sqlstr = #sqlstr + ' AND Attachment.PrivateSchoolID = PrivateSchool.PrivateSchoolID'
Else
SET #sqlstr = #sqlstr + ' AND Attachment.PrivateSchoolID is Null'
SET #sqlstr = #sqlstr + ' Group by SchoolName, Attachment.PrivateSchoolID, PrivateSchool.PrivateSchoolID'
SET #sqlstr = #sqlstr + ' Order By SchoolName'
EXEC(#sqlstr)
END
ELSE
Select 'No Data Found' as 'FileUpload'
END
You need:
EXEC sp_executesql #sqlstr, N'#intCount INT OUTPUT', #intCount = #intCount OUTPUT;
IF (#intCount > 0)
BEGIN
....
END
You'll also need to make #sqlstr NVARCHAR(2000) and add set it to N'SELECT ...' as opposed to 'SELECT ...' - that leading N can be important.
The problem is:
declare #intCount int
set #intCount = 0
...
<a bunch of code where #intcount doesn't change>
If (#intCount > 0)
It's always going to be 0.
Your issue is one of scope. The EXEC(#sqlstr) command doesn't have access to the #intcount variable in your stored procedure. I would bet if you ran this code in a query window, it would tell you to declare #intcount.
Listen to YUCK and rewrite this to avoid dynamic SQL, and then your SELECT will be able to set the #intcount variable.