SQL Server sending blank email - sql

For testing purposes, I did not pass any statement. But, I still get a blank email (test email comes in just fine). I tried the same code on different other servers and it works and prints the information. Below is the code. I added PRINT statement to see if shows me anything, no luck.
DECLARE #FQDN varchar (25)
DECLARE #Domain NVARCHAR(100)
DECLARE #EventTime NVARCHAR (25)
DECLARE #Instance NVARCHAR(50)
DECLARE #NLCR AS CHAR(2) = CHAR(13) + CHAR(10)
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',
'SYSTEM\CurrentControlSet\services\Tcpip\Parameters', N'Domain',#Domain OUTPUT
SET #FQDN = CAST(SERVERPROPERTY('MachineName') as nvarchar) + '.' + #Domain
SET #Instance = ##SERVICENAME
SET #EventTime = GETDATE()
DECLARE #strsubject varchar(100)
SELECT #strsubject = 'JDE Submitted Jobs in Error'
DECLARE #tableHTML nvarchar(max);
SET #tableHTML =
N'<H1>JDE Submitted Jobs in Error </H1>' + #NLCR +
N'<b>Server Name: </b>'+ #FQDN + #NLCR +' <br>' +
N'<b>Instance Name: </b>'+ #Instance + #NLCR +' <br>' +
N'<b>Event Time: </b>' + #EventTime + #NLCR +' <br>' +
N'<br>' +
N'<table border="1">' +
N'<tr><th>Queue</th><th>Status</th>' +
N'<th>Env</th><th>User</th>' +
N'<th>Job #</th><th>Description</th>' +
N'<th>Actual Date</th><th>Actual Time</th>' +
N'<th>Host</th></tr>' +
N'</table>'
;
PRINT #tableHTML

Related

Conversion failed when converting the varchar value ' and f.FUNCTION_ID= CAST(' to data type int

I am trying to make the procedure where I can get the data from MS SQL. Here I am getting the error
Msg 245, Level 16, State 1, Procedure GET_FUNCTIONS, Line 15 [Batch Start Line 33]
Conversion failed when converting the varchar value ' and f.FUNCTION_ID in '' to data type int.
CREATE procedure [dbo].[GET_FUNCTIONS]
#Function_Id int,
#EntityId int,
#OrderBy varchar(10)
as
begin
declare #Sql nvarchar(max)
if(#Function_Id=0 AND #EntityId=0)
set #Sql = 'select
f.Function_Code,f.FUNCTION_ID,f.EntityId,be.ENTITY_SHORT_NAME,f.FUNCTION_NAME,
f.FUNCTION_DESC,f.CREATED_DATE,f.MODIFIED_DATE from FUNCTIONS f
inner join BusinessEntity be on f.EntityId=be.ID
WHERE f.ACTIVE=1 '
if(#Function_Id!=0)
set #Sql += ' and f.FUNCTION_ID in ''' + #Function_Id + ''''
if(#EntityId!=0)
set #Sql += ' and f.EntityId = cast('+convert(varchar(12) ,#EntityId)+') '
set #Sql += ' order by FUNCTION_ID '+ #OrderBy
print #Sql
end
GO
Thanks everyone for your answers. I have got the answer by my own.
I have removed if(#Function_Id=0 AND #EntityId=0) and changed the data type of #Function_Id and #EntityId to varchar(100), so solved all errors.
I'm, making a few guesses here, which I add some comments about, however, this should be what you are after. I firstly ensure that I parametrise the values, but also validate and quote the value of #OrderBy:
CREATE PROCEDURE [dbo].[GET_FUNCTIONS] #Function_Id int =NULL, --Instead of passing 0as "select all" use NULL
#EntityId int = NULL, --Instead of passing 0as "select all" use NULL
#OrderBy nvarchar(4) --guessing this is mean to be ASC or DESC?
AS
BEGIN
IF #OrderBy NOT IN (N'ASC','DESC')
--Error is not ASC or DESC
THROW 51473, N'#OrderBy can only have a value of ''ASC'' or ''DESC''.',16; --51473 was a random error number; you can use one that is appropriate for your environment.
DECLARE #SQL nvarchar(MAX),
#CRLF nchar(2) = NCHAR(13) + NCHAR(10);
SET #SQL = N'SELECT f.Function_Code,' + + #CRLF +
N' f.FUNCTION_ID,' + + #CRLF +
N' f.EntityId,' + + #CRLF +
N' be.ENTITY_SHORT_NAME,' + + #CRLF +
N' f.FUNCTION_NAME,' + + #CRLF +
N' f.FUNCTION_DESC,' + + #CRLF +
N' f.CREATED_DATE,' + + #CRLF +
N' f.MODIFIED_DATE' + + #CRLF +
N'FROM FUNCTIONS f' + + #CRLF +
N' INNER JOIN BusinessEntity be ON f.EntityId = be.ID' + + #CRLF +
N'WHERE f.ACTIVE = 1';
IF (#Function_Id IS NOT NULL)
SET #SQL = #SQL + #CRLF + N' AND f.FUNCTION_ID = #Function_Id'
IF (#EntityId IS NOT NULL)
SET #SQL = #SQL + #CRLF + N' AND f.EntityId = #EntityId'
SET #SQL = #SQL + #CRLF +
N'ORDER BY F.FUNCTION_ID ' + QUOTENAME(#OrderBy) + N';'
--PRINT #SQL; --Uncomment for debugging.
EXEC sp_executesql #SQL, N'#Function_Id int, #EntityId int', #Function_Id, #EntityId;
END;
GO

Conversion failed when converting the varchar value Error in SQL

I am trying to create a dynamic SQL query and perform insert operation. But for some reason, I am getting
error: Conversion failed when converting the varchar value '
Please can anyone help me in resolving this error. Getting error at values('#file_type_id'
Below is my SQL code:
DECLARE #file_name VARCHAR(100) = 'MyFile'
DECLARE #file_type_id INT = 1
DEclare #filing_id bigint = 57
DECLARE #created_at DATETIME = GETDATE()
DECLARE #created_by BIGINT = 2
DECLARE #is_confidential bit = 1
set #insertquery =
'
DECLARE #Document AS VARBINARY(MAX)
SELECT #Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ''C:\SampleTestFiles\MyWordDoc.doc'', SINGLE_BLOB ) AS Doc
Insert ['+#databasename+'].['+#schemaname+'].['+#tablename+']
( [file_type_id], [file], [file_name], [filing_id], [created_at], [created_by], [is_confidential])
values ( '#file_type_id', #Document, #file_name, #filing_id , #created_at, #created_by, #is_confidential)
'
exec (#insertquery)
I suspect this is what you are after. Note that I have parametrised the variables, and safely quoted the values of your dynamic objects:
DECLARE #databasename sysname,
#schemaname sysname,
#tablename sysname;
DECLARE #file_name varchar(100) = 'MyFile',
#file_type_id int = 1,
#filing_id bigint = 57,
#created_at datetime = GETDATE(),
#created_by bigint = 2,
#is_confidential bit = 1,
#CRLF nchar(2) = NCHAR(13) + NCHAR(10),
#insertquery nvarchar(MAX);
SET #insertquery = N'DECLARE #Document AS VARBINARY(MAX);' + #CRLF +
N'SELECT #Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ''C:\SampleTestFiles\MyWordDoc.doc'', SINGLE_BLOB ) AS Doc;' + #CRLF + #CRLF +
N'INSERT INTO ' + QUOTENAME(#databasename) + '.' + QUOTENAME(#schemaname) + N'.' + QUOTENAME(#tablename) + N' ( [file_type_id], [file], [file_name], [filing_id], [created_at], [created_by], [is_confidential])' + #CRLF +
N'VALUES (#file_type_id, #Document, #file_name, #filing_id , #created_at, #created_by, #is_confidential);';
--PRINT #insertquery; --Your best friend.
EXEC sp_executesql #insertquery, N'#file_name varchar(100),#file_type_id int,#filing_id bigint,#created_at datetime, #created_by bigint,#is_confidential bit', #file_name, #file_type_id, #filing_id, #created_at, #created_by, #is_confidential;
I can't test the above, however, if you get any errors I would suggest looking at your best friend. if, however, I use the declarations below...:
DECLARE #databasename sysname = N'MyDB',
#schemaname sysname = N'dbo',
#tablename sysname = N'MyTable';
I get this statement, which appears to be correct:
DECLARE #Document AS VARBINARY(MAX);
SELECT #Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK 'C:\SampleTestFiles\MyWordDoc.doc', SINGLE_BLOB ) AS Doc;
INSERT INTO [MyDB].[dbo].[MyTable] ( [file_type_id], [file], [file_name], [filing_id], [created_at], [created_by], [is_confidential])
VALUES (#file_type_id, #Document, #file_name, #filing_id , #created_at, #created_by, #is_confidential);
Edit: to address the comment from the OP "How do I put another variable for file path." the correct syntax of that would be to make use of the second parameter from QUOTENAME (note that this strongly assumes that #filepath can never have a length longer than 128 characters):
SET #insertquery = N'DECLARE #Document AS VARBINARY(MAX);' + #CRLF +
N'SELECT #Document = CAST(bulkcolumn AS VARBINARY(MAX)) FROM OPENROWSET( BULK ' + QUOTENAME(#filepath,'''') + N', SINGLE_BLOB ) AS Doc;' + #CRLF + #CRLF +
N'INSERT INTO ' + QUOTENAME(#databasename) + '.' + QUOTENAME(#schemaname) + N'.' + QUOTENAME(#tablename) + N' ( [file_type_id], [file], [file_name], [filing_id], [created_at], [created_by], [is_confidential])' + #CRLF +
N'VALUES (#file_type_id, #Document, #file_name, #filing_id , #created_at, #created_by, #is_confidential);';

Population of Visual Studio Database Project data-loading scripts from existing data

So, I've been hunting for a solution for this for awhile and have come up with what sort of works ... but I can't help feeling that there must be something more elegant.
What I'm looking for is to be able to extract existing data from a populated database & incorporate that data into loading scripts. The database schema & configuration data will be rolled out multiple times, and will change as development continues, so it's important to be able to rebuild the configuration data from existing data, rather than from static data kept in scripts.
Here's what I've cobbled together:
create procedure #dump (
#TableName varchar(128)
)
as
set nocount on
set rowcount 0
declare #template varchar(max)
set #template = 'SET IDENTITY_INSERT [dbo].[' + #TableName + '] ON
MERGE INTO [dbo].[' + #TableName + '] AS [Target]
USING
(
VALUES
{vals}
)
AS [Source] ({fields})
ON [Target].[{pk}] = [Source].[{pk}]
WHEN MATCHED THEN UPDATE SET
{upds}
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
{fields}
)
VALUES
(
{fields}
);
SET IDENTITY_INSERT [dbo].[' + #TableName + '] OFF
--------------------------------------------------
'
declare #pk varchar(max) = ''
declare #vals varchar(max) = '/*
set concat_null_yields_null off
select ''' + '(' + ''' + replace(replace({casts} + '') ,'', '',,'', '', null,''), ''' + ',)' + ''', ''' + ',null)' + ''') from [' + #TableName + ']
*/'
declare #casts varchar(max) = ''
declare #fields varchar(max) = ''
declare #upds varchar(max) = ''
declare #inserts varchar(max) = ''
set #pk = SUBSTRING(#TableName, 1, len(#TableName) - 1) + 'ID'
declare cur_flds
cursor for select c.name, c.type
from syscolumns c
where c.id = object_id(#TableName)
order by c.colid
declare #fn varchar(max)
declare #ft int
open cur_flds
fetch next from cur_flds into #fn, #ft
while ##FETCH_STATUS = 0
begin
if len(#fields) > 0
set #fields = #fields + ', '
set #fields = #fields + '[' + #fn + ']'
if len(#casts) > 0
set #casts = #casts + ' + ' + ''','' + '
if #ft in(56,55,50,38,48)
set #casts = #casts + 'cast([' + #fn + '] as varchar)'
else if #ft = 111
set #casts = #casts + ''''''''' + ' + 'cast([' + #fn + '] as varchar) + ' + ''''''''''
else
set #casts = #casts + ''''''''' + ' + 'replace([' + #fn + '], ''''''''' + ', ' + ''''''''''''') + '''''''''
if #fn != #pk
begin
if len(#upds) > 0
set #upds = #upds + ', '
set #upds = #upds + '[Target].[' + #fn + '] = [Source].[' + #fn + ']'
end
fetch next from cur_flds into #fn, #ft
end
close cur_flds
deallocate cur_flds
set #vals = REPLACE(#vals, '{casts}', #casts)
set #template = REPLACE(#template, '{pk}', #pk)
set #template = REPLACE(#template, '{vals}', #vals)
set #template = REPLACE(#template, '{fields}', #fields)
set #template = REPLACE(#template, '{upds}', #upds)
set #template = REPLACE(#template, '{inserts}', #inserts)
print #template
go
exec #dump 'ActionItemSystems'
drop proc #dump
That ends up giving me output of:
SET IDENTITY_INSERT [dbo].[ActionItemSystems] ON
MERGE INTO [dbo].[ActionItemSystems] AS [Target]
USING
(
VALUES
/*
set concat_null_yields_null off
select '(' + replace(replace(cast([ActionItemSystemID] as varchar) + ',' + '''' + replace([ActionItemSystemName], '''', '''''') + '''' + ') ,', ',,', ', null,'), ',)', ',null)') from [ActionItemSystems]
*/
)
AS [Source] ([ActionItemSystemID], [ActionItemSystemName])
ON [Target].[ActionItemSystemID] = [Source].[ActionItemSystemID]
WHEN MATCHED THEN UPDATE SET
[Target].[ActionItemSystemName] = [Source].[ActionItemSystemName]
WHEN NOT MATCHED BY TARGET THEN
INSERT
(
[ActionItemSystemID], [ActionItemSystemName]
)
VALUES
(
[ActionItemSystemID], [ActionItemSystemName]
);
SET IDENTITY_INSERT [dbo].[ActionItemSystems] OFF
From this point, I can take the commented-out bit
set concat_null_yields_null off
select '(' + replace(replace(cast([ActionItemSystemID] as varchar) + ',' + '''' + replace([ActionItemSystemName], '''', '''''') + '''' + ') ,', ',,', ', null,'), ',)', ',null)') from [ActionItemSystems]
execute that, and get output like:
(33,'7111-5 -Upstream/Seed Lab') ,
(32,'7301-Seed Lab') ,
(30,'7807 UFDF') ,
(14,'BAS Panel Upgrade') ,
(1,'Clean Steam') ,
(13,'DCS') ,
(2,'HWFI') ,
(3,'MCS') ,
(12,'MES') ,
(31,'Seed Lab') ,
(18,'UCS WRO') ,
(34,'Upstream Seed Lab') ,
(29,'Viral Filtration') ,
which can then be incorporated (sans the final comma) into the script.
Now, this solution functions, but it's fragile. It depends on various assumptions (e.g., that the Table Name will have a Primary Key of Table Name - trailing 's' and plus ID) that may not hold true for every solution. It also requires cutting & pasting, and rerunning from the start when the table structures change.
This is probably quite a lot of background ... which I'm partly sharing because I couldn't find anything similar out there & thought that somebody might benefit from this. However, I still come back to my real question, which is to say: where's the tool to generate this kind of script for VS Database Projects? There really should be something - something that would take into account whatever the primary key is, that would generate the thing entire, etc.
You can try with this procedure for generating MERGE statements:
https://github.com/readyroll/generate-sql-merge
It is more advanced version of what you already have.

How to pass datetime in dynamic query in sql?

I have written a stored procedure like this
ALTER PROCEDURE [dbo].[spLoadPendingPaymentSheetByFilter] --'2015-04-01','2015-04-02','Select-One','Select-One','Select-One',''
#FromDate as datetime,
#ToDate as datetime,
#Status as nvarchar(50),
#Remarks as nvarchar(50),
#Paymenttype as nvarchar(50),
#BillID as nvarchar(50)
AS
Declare #Where as nvarchar(max)
set #Where = '( MenifestDate BETWEEN ''' + CONVERT(VARCHAR(10),#FromDate, 101) + ''' and ''' + CONVERT(VARCHAR(10),#ToDate, 101) + ''' )'
if(#Status <> 'Select-One')
set #Where = 'Status = '+ #Status
if(#Remarks <> 'Select-One')
set #Where = #Where + 'and Remarks = '+ #Remarks
if(#Paymenttype <> 'Select-One')
set #Where = #Where + 'and PaymentType = ' + #Paymenttype
if(#BillID <> '')
set #Where = #Where + 'and BillID = '+ #BillID
Declare #SelectString as nvarchar(1000)
set #SelectString = 'SELECT MasterID,BillID, MenifestDate, FarwardingNo,ReceverCountryName,Status,Remarks,PaymentType
FROM tblMenifest
WHERE ' + #Where
exec #SelectString
When I execute it I got this error
The name 'SELECT MasterID,BillID, MenifestDate, FarwardingNo,ReceverCountryName,Status,Remarks,PaymentType FROM tblMenifest WHERE ( MenifestDate BETWEEN '04/01/2015' and '04/02/2015' )' is not a valid identifier
The MenifestDate column datatype is datetime.
I believe that you want to put EXEC(#SelectString) rather than exec #SelectString.

SQL Server 2008 query into text file: bcp, dynamic SQL, temp tables and table variables

I am adding a section to some vendor code to create a text file from data in a temp table and send it to a network location. The temp table is #PickList
I've created a table type for the table variable, so I can pass it as a parameter to the dynamic SQL, but this in turn needs to be passed to bcp which creates the text file. This is what I have so far:
DECLARE #strFileLocation VARCHAR(1000)
DECLARE #strFileName VARCHAR(1000)
DECLARE #bcpCommand VARCHAR(8000)
DECLARE #strSQL VARCHAR(2000)
SET #strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'
SET #strFileName = 'BPL' + #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
VARCHAR(10)) + CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'
DECLARE #tblLeanLiftData AS [dbo].[BWT_LeanLiftPickTableType]
INSERT INTO #tblLeanLiftData (intSeq, strText)
SELECT 0, #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20))
UNION
SELECT det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM #PickList
SET #strSQL = 'EXEC sp_executesql N''SELECT strText FROM #tblLeanLiftData
ORDER BY intSeq'', N''#tblLeanLiftData LeanLiftPickTableType READONLY'',
#tblLeanLiftData=#tblLeanLiftData'
SET #bcpCommand = 'bcp "' + #strSQL + '" queryout "'
SET #bcpCommand = #bcpCommand + #strFileLocation + #strFileName + '" -T -c'
EXEC master..xp_cmdshell #bcpCommand
When I exec the code, I get:
SQLState = 37000, NativeError = 137
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Must declare the scalar variable "#tblLeanLiftData".
SQLState = 37000, NativeError = 8180
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Statement(s) could not be prepared.
NULL
So, the table variable is still out of scope.
I was hoping someone with fresh eyes could spot where I've gone wrong or an alternative route or if it's just not possible this way?
It's a bit of a double-edged sword, because I've tried various ways and either the temp table or table variable ends up being out of scope.
can you please try this?
DECLARE #strFileLocation VARCHAR(1000)
DECLARE #strFileName VARCHAR(1000)
DECLARE #bcpCommand VARCHAR(8000)
DECLARE #strSQL VARCHAR(2000)
SET #strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'
SET #strFileName = 'BPL' + #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
VARCHAR(10)) + CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'
INSERT INTO [dbo].[BWT_LeanLiftPickTableType] (intSeq, strText)
SELECT 0, #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20))
UNION
SELECT det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM #PickList
SET #strSQL = 'EXEC sp_executesql N''SELECT strText FROM [dbo].[BWT_LeanLiftPickTableType]
ORDER BY intSeq'', N''[dbo].[BWT_LeanLiftPickTableType] LeanLiftPickTableType READONLY'',
[dbo].[BWT_LeanLiftPickTableType]=[dbo].[BWT_LeanLiftPickTableType]'
SET #bcpCommand = 'bcp "' + #strSQL + '" queryout "'
SET #bcpCommand = #bcpCommand + #strFileLocation + #strFileName + '" -T -c'
EXEC master..xp_cmdshell #bcpCommand
If this suceeds, then the only issue is that you tried to use a variable as a column name.
Also as I have seen from your query it is not completely necessary to use the table name as a variable, as you declare and assign it immediately, so you could just as well write your column there. A thing to add if you choose to do this way is to truncate the table at the end of the query.
If the intention was to create a table variable, it should be assigned like this:
DECLARE #strFileLocation VARCHAR(1000)
DECLARE #strFileName VARCHAR(1000)
DECLARE #bcpCommand VARCHAR(8000)
DECLARE #strSQL VARCHAR(2000)
SET #strFileLocation = '\\phaal\FTP\LocalUser\LIFT01\inbox\'
SET #strFileName = 'BPL' + #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20)) + CAST(DATEPART(hh,GETDATE()) AS
VARCHAR(10)) + CAST(DATEPART(mi,GETDATE()) AS VARCHAR(10)) +
CAST(DATEPART(ss,GETDATE()) AS VARCHAR(10)) + '.txt'
DECLARE #tblLeanLiftData TABLE (intSeq int, strText VARCHAR(MAX))
INSERT INTO #tblLeanLiftData (intSeq, strText)
SELECT 0, #Job + '-' + CAST(#Suffix AS VARCHAR(20)) + '-' +
CAST(#StartingOperNum AS VARCHAR(20))
UNION
SELECT det_JobSequence, det_JobMatlItem + ':' + det_LotDescription + ',-,' +
CAST(det_QtyToPick AS VARCHAR(20))
FROM #PickList
SET #strSQL = 'EXEC sp_executesql N''SELECT strText FROM #tblLeanLiftData
ORDER BY intSeq'', N''#tblLeanLiftData LeanLiftPickTableType READONLY'',
#tblLeanLiftData=#tblLeanLiftData'
SET #bcpCommand = 'bcp "' + #strSQL + '" queryout "'
SET #bcpCommand = #bcpCommand + #strFileLocation + #strFileName + '" -T -c'
EXEC master..xp_cmdshell #bcpCommand
I hope this answer will help you to move forward.