问题
I have a handful of queries that I'm using to test whether or not database results from tests that I run don't have anything obviously broken and buggy in them.
One of the queries has this basic form:
SELECT *
FROM Table
WHERE Column = ''
Its checking to make sure that one particular field is not blank. I want to keep track of these tests being run and when they pass or fail. Is there a way that I can write a query so that if that query brings back any results then it writes a string to another table that says something like "Test Passed"?
So, a pseudo version might look something like this:
IF (
SELECT *
FROM Table1
WHERE Table1.Column1 = ''
) = 0
INSERT INTO Table2 (FileName, Date, Result)
VALUES ('File1', 'GetDate()', 'Test Passed')
ELSE
INSERT INTO Table2 (FileName, Date, Result)
VALUES ('File1', 'GetDate()', 'Test Failed')
And the table might look like this:
| FileName | Date | Result |
|:--------:|:--------:|:-----------:|
| File1 | 12-25-16 | Test Passed |
| File2 | 12-25-16 | Test Failed |
| File3 | 12-25-16 | Test Passed |
| File4 | 12-25-16 | Test Passed |
回答1:
You almost have it in your code. Just change the SELECT *
to SELECT COUNT(*)
and you've got it . I would set the status and then do the insert.
DECLARE @testStatus NVARCHAR(MAX);
IF (
SELECT COUNT(*)
FROM Table1
WHERE Table1.Column1 = ''
) = 0
SET @testStatus = 'Test Passed'
ELSE
SET @testStatus = 'Test Failed'
INSERT INTO Table2 (FileName, Date, Result)
VALUES ('File1', GetDate(), @testStatus)
回答2:
Is there a way that I can write a query so that if that query brings back any results then it writes a string to another table that says something like "Test Passed"?
Building further on your query,you can do something like below
--No columns so test passed
IF not exists ( SELECT *
FROM Table1
WHERE Table1.Column1 = ''
)
Begin
INSERT INTO Table2 (FileName, Date, Result)
VALUES ('File1', 'GetDate()', 'Test Passed')
End
回答3:
If you want to achieve the result only with a query (without T-SQL) you can use this solution:
INSERT INTO Table2(FileName, Date, Result)
SELECT 'File1', GetDate(), X.Result FROM (
SELECT 'Test passed' Result
WHERE EXISTS(SELECT * FROM Table1 WHERE Column1 = '')
UNION ALL
SELECT 'Test failed'
WHERE NOT EXISTS (SELECT * FROM Table1 WHERE Column1 = '')
) X
With a little effort you can write also the Oracle version of the same query:
INSERT INTO Table2(FileName, Date, Result)
SELECT 'File1', sysdate, X.Result FROM (
SELECT 'Test passed' Result FROM dual
WHERE EXISTS(SELECT * FROM Table1 WHERE Column1 = '')
UNION ALL
SELECT 'Test failed' FROM dual
WHERE NOT EXISTS (SELECT * FROM Table1 WHERE Column1 = '')
) X
回答4:
MERGE
table2 AS target
USING
table1 AS source
ON
source.Column1 = ''
WHEN MATCHED THEN
INSERT INTO target (FileName, Date, Result)
VALUES ('File1', 'GetDate()', 'Test Failed')
WHEN NOT MATCHED THEN
INSERT INTO target (FileName, Date, Result)
VALUES ('File1', 'GetDate()', 'Test Passed')
来源:https://stackoverflow.com/questions/37660990/inserting-strings-into-an-sql-table-depending-on-what-results-a-query-returns