如何在批处理文件中获取 SQL Server 脚本错误

问题描述 投票:0回答:2

我在批处理文件上调用 SQL Server 脚本,但我需要在批处理文件中获取错误(当脚本失败时),我该怎么办?

这是批处理文件:

sqlcmd -S HOST  -U User -P password -i test.sql
echo %errorlevel%

这是脚本文件(Test.sql):

USE TrainingSitecore_Web  
go

SELECT * FROM   items 
go

RAISERROR ('This is a test Error.',-- Message text.
           16,-- Severity.
           1 -- State.
); 

有道理吗?

sql-server batch-file error-handling sql-scripts
2个回答
0
投票

可以使用-r参数来完成:

示例:

sqlcmd -Q "select 1 as a; select 1/0 as b" -E -r1 1> NUL

-r[ 0 | 1] 发送至 stderr 的消息

将错误消息输出重定向到屏幕(stderr)。如果你这样做 不指定参数或者如果指定 0,则仅显示错误消息 严重级别为 11 或更高的将被重定向。如果您指定 1、所有错误消息输出包括PRINT都被重定向。没有 如果你使用-o 效果。默认情况下,消息发送到标准输出。

MSDN 了解更多


0
投票
    -- Create table for departments
CREATE TABLE departments (
    id INT PRIMARY KEY IDENTITY,
    department_name VARCHAR(100)
);

-- Create table for employees
CREATE TABLE employees (
    id INT PRIMARY KEY IDENTITY,
    employee_name VARCHAR(100),
    fk_department_id INT FOREIGN KEY REFERENCES departments(id)
);

-- Create table for leave types
CREATE TABLE leave_types (
    id INT PRIMARY KEY IDENTITY,
    leave_type VARCHAR(100),
    max_leaves_per_month FLOAT
);

-- Create table for leave records
CREATE TABLE leave_records (
    id INT PRIMARY KEY IDENTITY,
    fk_employee_id INT FOREIGN KEY REFERENCES employees(id),
    leave_date DATE,
    comments VARCHAR(255),
    fk_approved_by_id INT FOREIGN KEY REFERENCES employees(id),
    fk_leave_type_id INT FOREIGN KEY REFERENCES leave_types(id)
);
GO

-- Create table for leave balance
CREATE TABLE leave_balance (
    fk_employee_id INT,
    leave_balance FLOAT,
    year INT,
    PRIMARY KEY (fk_employee_id, year),
    FOREIGN KEY (fk_employee_id) REFERENCES employees(id)
);
GO

-- Query to generate report
SELECT 
    e.employee_name,
    l.leave_date,
    l.comments,
    ea.employee_name AS approved_by,
    lt.leave_type
FROM 
    leave_records l
JOIN 
    employees e ON l.fk_employee_id = e.id
JOIN 
    employees ea ON l.fk_approved_by_id = ea.id
JOIN 
    leave_types lt ON l.fk_leave_type_id = lt.id
WHERE 
    YEAR(l.leave_date) = YEAR(GETDATE())
ORDER BY 
    l.leave_date;

-- Query to generate department-wise report
SELECT 
    d.department_name,
    e.employee_name,
    l.leave_date,
    l.comments,
    ea.employee_name AS approved_by,
    lt.leave_type
FROM 
    leave_records l
JOIN 
    employees e ON l.fk_employee_id = e.id
JOIN 
    departments d ON e.fk_department_id = d.id
JOIN 
    employees ea ON l.fk_approved_by_id = ea.id
JOIN 
    leave_types lt ON l.fk_leave_type_id = lt.id
WHERE 
    l.leave_date BETWEEN GETDATE() AND DATEADD(MONTH, 1, GETDATE())
ORDER BY 
    l.leave_date;
GO

-- Create function to calculate leaves
CREATE FUNCTION calculate_leaves(@joining_date DATE)
RETURNS INT
AS
BEGIN
    DECLARE @months INT;
    SET @months = DATEDIFF(MONTH, @joining_date, DATEADD(YEAR, 1, @joining_date));
    RETURN @months * 1.5;
END;
GO

-- Stored procedure to add leave balance
CREATE PROCEDURE add_leave_balance
    @employee_id INT,
    @joining_date DATE
AS
BEGIN
    DECLARE @leaves FLOAT;
    DECLARE @year INT;
    SET @leaves = dbo.calculate_leaves(@joining_date);
    SET @year = YEAR(@joining_date);
    INSERT INTO leave_balance (fk_employee_id, leave_balance, year) VALUES (@employee_id, @leaves, @year);
END;
GO

-- Create view for approved leave details
CREATE VIEW approved_leave_details AS
SELECT 
    e.employee_name,
    l.leave_date,
    l.comments,
    ea.employee_name AS approved_by,
    lt.leave_type
FROM 
    leave_records l
JOIN 
    employees e ON l.fk_employee_id = e.id
JOIN 
    employees ea ON l.fk_approved_by_id = ea.id
JOIN 
    leave_types lt ON l.fk_leave_type_id = lt.id
WHERE 
    l.leave_date BETWEEN GETDATE() AND DATEADD(MONTH, 1, GETDATE())
ORDER BY 
    l.leave_date;
GO

-- Trigger for new leave type addition
CREATE TRIGGER add_leave_type_trigger
ON leave_types
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @default_balance FLOAT;
    SELECT @default_balance = max_leaves_per_month FROM inserted;
    UPDATE leave_balance 
    SET leave_balance = leave_balance + (12 - MONTH(GETDATE()) + 1) * @default_balance;
END;
GO

-- Create trigger to automatically add leave balance for new employees
CREATE TRIGGER trg_add_leave_balance
ON employees
AFTER INSERT
AS
BEGIN
    SET NOCOUNT ON;
    INSERT INTO leave_balance (fk_employee_id, leave_balance, year)
    SELECT id, dbo.calculate_leaves(GETDATE()), YEAR(GETDATE()) FROM inserted;
END;
GO


-- Insert data into departments
INSERT INTO departments (department_name) VALUES ('HR'), ('Development'), ('Marketing');

-- Insert data into employees
INSERT INTO employees (employee_name, fk_department_id) VALUES 
('Krunal', 1), 
('Panth', 2), 
('Parth', 3);

-- Insert data into leave_types
INSERT INTO leave_types (leave_type, max_leaves_per_month) VALUES 
('Holiday', 2.5), 
('Sick', 1.5), 
('Planned', 1.0);

-- Insert data into leave_records
INSERT INTO leave_records (fk_employee_id, leave_date, comments, fk_approved_by_id, fk_leave_type_id) VALUES 
(1, '2025-01-10', 'This is Annual leave', 2, 1), 
(2, '2025-02-15', 'This is Sick leave', 3, 2), 
(3, '2025-03-20', 'This is Personal leave', 1, 3);

-- For testing, you can insert a new leave type to trigger the `add_leave_type_trigger`
INSERT INTO leave_types (leave_type, max_leaves_per_month) VALUES ('Exam', 1);

SELECT * FROM leave_balance;
SELECT * FROM employees;
© www.soinside.com 2019 - 2024. All rights reserved.