반응형

MSSQL 에서 메일 보내기가 가능합니다.

SMTP사용을 위한 메일정보가 필요하며 해당 계정을 MSSQL에 셋팅하여 메일 보내기를 합니다.

 

1. MSSQL에 메일 정보 셋팅

    - SSMS 로 접속
    - DB 하단의 "관리" 메뉴 하단 "데이터베이스 메일" 더블클릭 후 메일정보 설정 진행

 

 

2. DB메일 발송 및 확인

-- 메일 발송

exec msdb.dbo.sp_send_dbmail
@profile_name = 'MSSQL DB Email 설정시 입력한 프로필 이름',
@recipients = '보내고자 하는 이메일 주소',
@subject = '제목',
@body = '내용',
@body_format = 'HTML' or 'TEXT';

 

-- 발송 내역 확인 성공 or 실패 or 발송 불가
SELECT sent_status, *
FROM msdb.dbo.sysmail_allitems
order by mailitem_id desc;

반응형
Posted by 투명강아지
,
반응형

mssql 에서 활성 중인 또는 실행 대기 중인 쿼리와 세션을 확인 하여 lock에 걸린 문제의 세션을 kill 하기.

 

--세션
SELECT sqltext.TEXT, req.session_id, req.status, req.start_time, req.command, req.cpu_time, req.wait_time, req.total_elapsed_time, text_size
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
order by sqltext.TEXT, req.status
;

-- lock
SELECT DISTINCT name AS database_name, session_id, host_name, login_time, login_name, reads, writes        
FROM    sys.dm_exec_sessions        
LEFT OUTER JOIN sys.dm_tran_locks ON sys.dm_exec_sessions.session_id = sys.dm_tran_locks.request_session_id        
INNER JOIN sys.databases ON sys.dm_tran_locks.resource_database_id = sys.databases.database_id
WHERE resource_type <> 'DATABASE'
AND request_mode LIKE '%X%'
--AND name ='YG1MES'
ORDER BY name 
;

--kill 은 서버에서
kill session_id
;

반응형
Posted by 투명강아지
,
반응형

컬럼에 identity 속성이 설정되어 있어서 임의적으로 데이터 삽입이 불가능.

 

insert into table_name

select * from table_name;

 

처리시 id값이 증가해야 함으로 컬럼명을 지정하지 않은 상태에서 데이터가 들어갈 수 없음.

 

SET IDENTITY_INSERT table_name ON

insert into table_name

select * from table_name

SET IDENTITY_INSERT table_name OFF

 

로 임시적으로 허용 후 데이터 처리 다시 테이블의 제약조건을 허용하여 처리가 가능함.

 

//** 2024.01.18 내용 추가 **//

위 건으로 처리 되지 않음.

SET IDENTITY_INSERT table_name ON

insert into table_name(column1, column2)

select column1, column2  from table_name

SET IDENTITY_INSERT table_name OFF

 

컬럼을 명시하여 처리해야 오류발생되지 않음.

 

아래는 mssql error내역에 대한 내용과 처리 관련한 내용을 표기함.

http://www.sql-server-helper.com/error-messages/msg-8101.aspx

 

SQL Server Helper - SQL Server Error Messages

Error Message Server: Msg 8101, Level 16, State 1, Line 2 An explicit value for the identity column in table "Table Name" can only be specified when a column list is used and IDENTITY_INSERT is ON. Causes This error happens when you are trying to insert a

www.sql-server-helper.com

 

SQL Server Error Messages - Msg 8101
Error Message
Causes
This error happens when you are trying to insert a new record into a table that contains an identity column without specifying the columns in the INSERT statement and you are assigning a value to the identity column instead of letting SQL Server assign the value.

To illustrate on how the error can be encountered:

CREATE TABLE [dbo].[Users] (
    [UserID]    INT NOT NULL IDENTITY(1, 1),
    [UserName]  VARCHAR(20) NOT NULL,
    [FirstName] VARCHAR(50) NOT NULL,
    [LastName]  VARCHAR(50) NOT NULL
)
GO

INSERT INTO [dbo].[Users] 
VALUES ( 1, 'superman', 'Clark', 'Kent' )
GO

Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 'dbo.Users' can only be specified when a 
column list is used and IDENTITY_INSERT is ON.

If you specified the column names in the INSERT statement, you will get a different error message:

INSERT INTO [dbo].[Users] ( [UserID], [UserName], [FirstName], [LastName] )
VALUES ( 1, 'superman', 'Clark', 'Kent' )
GO

Msg 544, Level 16, State 1, Line 2
Cannot insert explicit value for identity column in table 'Users' when 
IDENTITY_INSERT is set to OFF.

Solution / Workaround:

There are two ways of avoiding any of the errors mentioned above.  The first option is not to include the identity column in the INSERT statement and let SQL Server assign the next identity value to the record:

INSERT INTO [dbo].[Users] ( [UserName], [FirstName], [LastName] )
VALUES ( 'superman', 'Clark', 'Kent' )
GO

The second option is to enable the IDENTITY_INSERT property for the table.  If you really want to specify the value for the identity column, this option is the one for you.

SET IDENTITY_INSERT [dbo].[Users] ON
GO

INSERT INTO [dbo].[Users] ( [UserID], [UserName], [FirstName], [LastName] )
VALUES ( 1, 'superman', 'Clark', 'Kent' )
GO

SET IDENTITY_INSERT [dbo].[Users] OFF
GO

Setting the IDENTITY_INSERT to ON for the table allows explicit values to be inserted into the identity column of a table.  At any given time, only one table in a session can have the IDENTITY_INSERT property set to ON.  If a table already has this property set to ON and a SET IDENTITY_INSERT ON statement is issued on another table, SQL Server will return the following error message:

Msg 8107, Level 16, State 1, Line 2
IDENTITY_INSERT is already ON for table 'dbo.Deparments'. 
Cannot perform SET operation for table 'dbo.Users'.

Execute permissions for the SET IDENTITY_INSERT default to the sysadmin fixed server role and the db_owner and db_ddladmin fixed database roles, and the object owner.

반응형
Posted by 투명강아지
,