We run SQL Server 2012. I'd like to generate an alert email if a condition is met. This would be setup in SQL Server Agent and run every hour.
Basically would run the following query:
SELECT
IBTRANSACTIONID, SUBCONSTATUS
FROM
PSAPMSGSUBCON
WHERE
SUBCONSTATUS = 3
AND STATUSSTRING = 'WRKNG'
IF --(need some input here)
EXEC msdb.dbo.sp_send_dbmail --(then would call this to issue email)
Appreciate replies
You may try this one.
DECLARE
#IBTRANSACTIONID VARCHAR(100) = NULL,
#SUBCONSTATUS VARCHAR(100) =NULL
SELECT
#IBTRANSACTIONID = IBTRANSACTIONID,
#SUBCONSTATUS = SUBCONSTATUS
FROM
PSAPMSGSUBCON
WHERE
SUBCONSTATUS = 3
AND STATUSSTRING = 'WRKNG'
IF (#IBTRANSACTIONID IS NOT NULL AND #IBTRANSACTIONID <>'') AND (#SUBCONSTATUS IS NOT NULL OR #IBTRANSACTIONID <>'')
BEGIN
EXEC msdb.dbo.sp_send_dbmail --(then would call this to issue email)
END
Related
I have a SQL Server stored procedure, which runs via a scheduled task every 7 minutes, and which is used to send an email to the recipients concerned, once the condition of the number of records returned by request is greater than or equal to 1.
the procedure code is:
Begin
declare #recordCount int
declare #bodytext varchar(200)
select #recordCount = isnull(count(*), 0)
from table where .............
IF (#recordCount > 0)
set #bodytext = N'Message'
EXEC msdb.dbo.sp_send_dbmail
#profile_name = 'Profile',
#recipients = 'Test#test.com',
#subject = 'Title',
#Body = #bodytext,
#query = N'Select Column1,Column2
from Table
inner join Table2 on .........
where ........................',
#execute_query_database = N'DB'
End
End;
My problem is that I'd like to send the email, only once, since the scheduled task runs every 7 minutes, but i can't find the track how i can achieve that without adding new columns in the "Table".
We are using SQL Server 2017.
When I tried to Configure an SSIS package, and set the value of a parameter to something other than the default value from the package, I am getting the error “Cannot find the parameter ‘myParm’ because it does not exist.”
Parameter ‘myParm’ does exist in the SSIS package.
This is the generated SQL:
EXEC [SSISDB].[catalog].[set_object_parameter_value] #object_type=30,
#parameter_name=N'myParm', #object_name=N'mySSIS.dtsx', #folder_name=N'myFolder',
#project_name=N'myProject', #value_type=R, #parameter_value=N'myParmValue'
The error is in this statement in the stored procedure set_object_parameter_value
SELECT #parameter_id = [parameter_id], #sensitive = [sensitive],
#parameter_data_type = [data_type]
FROM [catalog].[object_parameters]
WHERE [project_id] = #project_id AND [object_type] = #object_type
AND [parameter_name] = #parameter_name COLLATE SQL_Latin1_General_CP1_CS_AS
AND [object_name] = #object_name
The weird thing is when I run this statement outside the stored procedure, it runs successfully and return a value for #parameter_id
DECLARE #sensitive bit
DECLARE #parameter_id bigint
DECLARE #parameter_data_type nvarchar(128)
SELECT #parameter_id = [parameter_id], #sensitive = [sensitive],
#parameter_data_type = [data_type]
FROM [catalog].[object_parameters]
WHERE [project_id] = 5 AND [object_type] = 30 AND [parameter_name] =
'myParm' COLLATE SQL_Latin1_General_CP1_CS_AS
AND [object_name] = 'mySSIS.dtsx'
SELECT #parameter_id
But, when I run this statement inside the stored procedure set_object_parameter_value, it gives the same error “Cannot find the parameter ‘myParm’ because it does not exist.”
When I run this statement inside the stored procedure set_object_parameter_value and comment out these 2 lines, it runs successfully
WITH EXECUTE AS 'AllSchemaOwner'
EXECUTE AS CALLER
I login to SSMS using Windows Authentication myDomain\myAccount
I have a custom database sql Trigger which inserts a new row if the previous condition has been satisfied.
The problem is, the confirmed_Erp flag on the database does not get actioned if I include the email section of the code below which I have commented out.
Do I need to add a delay or how could I rewrite this to ensure that the confirmed_Erp flag is set to 1 and therefore the email will send. one of the email conditions is confirmed_erp = 1
There is an app which is running on top of the database which runs jobs and once a job completed the confirmed_erp flag in the database changes to 1.
Code:
begin
select #new_id = max(id) from jobr_generic;
insert jobr_generic (id, type, jobr_ts, customerno, str_attr1,str_attr3,
dec_attr1, str_attr4,str_attr5, str_attr6,
str_attr7,str_attr8,str_attr9,str_attr10,str_attr11,str_attr12,str_attr13,str_attr14,str_attr15,str_attr16,str_attr17,str_attr18,str_attr19,str_attr20, send_erp,species, maincut, subcut, meatgroup, packtype, factorycode, supplyingsite, currency)
values (#new_id+1, '235', getdate(), #customerno,#attr1, #attr3, #decattr1, #attr4, #attr5, #attr6, #attr7, #attr8, #attr9, #attr10, #attr11, #attr12, #attr13, #attr14, #attr15, #attr16, #attr17, #attr18, #attr19, #attr20, 1, #species,#maincut,#subcut,#meatgroup,#packtype,#factorycode,#supplyingsite,#currency);
end
if #type ='235' AND #CONFIRMED_ERP = 1 AND #itemrequestfor = 'P'
begin
declare #body2 varchar(1024)
set #body2 = 'Primal Item: '+#attr16+ ' has been created and is ready
for use.'
exec msdb.dbo.sp_send_dbmail
#profile_name = 'DoNotReplyMailProfile',
#recipients= 'test#test.com',
#copy_recipients= 'test1#test.com',
#subject = 'A Primal Item has been Created',
#body = #body2,
#body_format = 'TEXT',
#exclude_query_output = 1 ;
end
i have a query that retrieve data from 2 differents server, but once i execute the query i am getting this error
Login failed for user 'usr1'
i am running the query in server1, with username: usr1
select userid, m.name, Cardid, m.dircode,
from [Server1].Database1.dbo.member m
JOIN [Server2].Database2.dbo.GetUserDetails p
on p.cprno = m.cardid
WHERE PerUserName='mftrame'
i already provide usr1 read permission on [Server2].Database2.dbo.GetUserDetails
also how can i check if i added a link to server1 into server2
From http://support.microsoft.com/kb/889615:
This problem occurs when the SQL Serversecurity authentication is set
to Windows only, and one of the following conditions is true: You are
trying to connect to a SQL Server database with a SQL Server
login.[...]
You need to enable SQL-Server logins on the other sql server.
Most-likely, the server currently only allows integrated-security logins.
Also check with SSMS if it actually works.
The SQL-Server account will also need to have the rights to access the database.
SELECT
ss.server_id
,ss.name
,ss.product
,ss.provider
,ss.data_source
,ss.is_linked
,ss.is_remote_login_enabled
,ss.is_rpc_out_enabled
,ss.is_data_access_enabled
,ss.uses_remote_collation
,ss.is_remote_proc_transaction_promotion_enabled
,ll.local_principal_id
,ll.uses_self_credential
,ll.remote_name
,sp.type_desc
,sp.default_database_name
,sp.default_language_name
FROM sys.linked_logins as ll
LEFT JOIN sys.server_principals as sp
ON sp.[principal_id] = local_principal_id
LEFT JOIN sys.servers AS ss
ON ss.server_id = ll.server_id
To enable mixed-mode authentication:
USE master
/* Mixed Authenication script SR 01-14-10, can update SQL authentication for instances.
works with SQL 05/08 not tested with SQL 05 instances*/
DECLARE #INSTANCEID VARCHAR(30)
DECLARE #STRVERSION VARCHAR(30)
DECLARE #SQLVERSION VARCHAR(30)
DECLARE #CMD VARCHAR(2000)
SET #SQLVERSION = (SELECT CONVERT(VARCHAR(20),(SERVERPROPERTY('productversion'))))
SET #INSTANCEID = ((SELECT CAST(SERVERPROPERTY('InstanceName') AS VARCHAR)))
IF (SELECT REPLACE(LEFT(#SQLVERSION,2),'.','')) = 10
BEGIN
SET #STRVERSION = 'MSSQL10'
END
ELSE
IF (SELECT REPLACE(LEFT(#SQLVERSION,2),'.','')) = 9
BEGIN
SET #STRVERSION = 'MSSQL'
END
IF #INSTANCEID IS NULL AND #STRVERSION = 'MSSQL'
BEGIN
SET #INSTANCEID = 1
END
ELSE
IF #INSTANCEID IS NULL AND #STRVERSION = 'MSSQL10'
BEGIN
SET #INSTANCEID = 'MSSQLSERVER'
END
SET #CMD = 'xp_regwrite ' + 'N' + '''HKEY_LOCAL_MACHINE''' + ',' + ' N' + '''Software\Microsoft\Microsoft SQL Server\'+ #STRVERSION +'.'+ #INSTANCEID + '\MSSQLServer'''+','+' N'+'''LoginMode'''+', '+'REG_DWORD'+','+ ' 2' --2 is mixed auth.
--EXEC(#CMD)
PRINT #CMD
--PRINT 'A restart of SQL is required for authentication changes to take effect'
Maybe an example does help:
To access table t_users on database catalog cor_basic on server cordb2005
with user ApertureWebServicesDE (on cordb2005) and password MY_TOP_SECRET_PASSWORD you would use:
EXEC master.dbo.sp_addlinkedserver
#server = N'RemoteDB'
,#srvproduct = 'OLE DB Provider for SQL'
,#provider = N'SQLNCLI'
,#datasrc = 'CORDB2005'
,#catalog = 'COR_Basic'
GO
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'RemoteDB'
,#useself = false
--,#locallogin = 'LocalIntegrationUser'
,#locallogin = NULL
,#rmtuser = N'ApertureWebServicesDE'
,#rmtpassword = N'MY_TOP_SECRET_PASSWORD'
GO
select *
from RemoteDB.COR_Basic.dbo.t_users
This of course preconditions that the login of ApertureWebServicesDE on CORDB2005 works, and that ApertureWebServicesDE has sufficient rights to access database COR_Basic and do a select on T_Users.
Restart required for the above changes to take effect !
Quite a simple question. In SQL 2008 if I have a stored procedure (see below) do I run the risk of a race condition between the first two statements or does the stored procedure put a lock on the things it touches like transactions do?
ALTER PROCEDURE [dbo].[usp_SetAssignedTo]
-- Add the parameters for the stored procedure here
#Server varchar(50),
#User varchar(50),
#UserPool varchar(50)
AS
BEGIN
SET NOCOUNT ON;
Declare #ServerUser varchar(50)
-- Find a Free record
SELECT top 1 #ServerUser = UserName
from ServerLoginUsers
where AssignedTo is null and [TsServer] = #Server
--Set the free record to the user
Update ServerLoginUsers
set AssignedTo = #User, AssignedToDate = getdate(), SourcePool = #UserPool
where [TsServer] = #Server and UserName = #ServerUser
--report record back if it was updated. Null if it was not available.
select *
from ServerLoginUsers
where [TsServer] = #Server
and UserName = #ServerUser
and AssignedTo = #User
END
You could get a race condition.
It can be done in one statement:
You can assign in an UPDATE
The lock hints allow another process to skip this row
The OUTPUT clause returns data to the caller
Try this... (edit: holdlock removed)
Update TOP (1) ServerLoginUsers WITH (ROWLOCK, READPAST)
OUTPUT INSERTED.*
SET
AssignedTo = #User, AssignedToDate = getdate(), SourcePool = #UserPool
WHERE
AssignedTo is null and [TsServer] = #Server -- not needed -> and UserName = #ServerUser
If not, you may need a separate select
Update TOP (1) ServerLoginUsers WITH (ROWLOCK, READPAST)
SET
-- yes, assign in an update
#ServerUser = UserName,
-- write
AssignedTo = #User, AssignedToDate = getdate(), SourcePool = #UserPool
OUTPUT INSERTED.*
WHERE
AssignedTo is null and [TsServer] = #Server -- not needed -> and UserName = #ServerUser
SELECT ...
See this please for more: SQL Server Process Queue Race Condition