I would like verify unique record in customer table and write the following query
declare @NT1 int
set @NT1 = (SELECT LogID, CustomerID, count(1)
FROM dbo.Customer
group by LogID,CustomerID
having count(1) >1)
if @NT1 > 1
print 'Fail'
ELSE
PRINT 'Pass';
I get the following message when run the query:
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
If you want to verify that no (LogId, CustomerId)
pair occurs more than once in the Customer
table, then you can do that.
The simplest way is:
create unique index idxu_customer_logid_customerid on customer(logid, customerid);
That way, the database ensures uniqueness. And, the rest of your code doesn't have to worry about it.
If you want to run your test, use exists
:
IF (EXISTS (SELECT LogID, CustomerID, count(1)
FROM dbo.Customer
GROUP BY LogID, CustomerID
HAVING count(1) > 1
)
)
BEGIN
PRINT 'Fail';
END
ELSE
BEGIN
PRINT 'Pass';
END;
DECLARE @Ncount int
set @Ncount = (select top 1
count(1)
FROM dbo.Customer
GROUP BY LogID, CustomerID
HAVING count(1) > 0
)
SELECT IIF ( @Ncount >= 1, 'Failed', 'Pass' ) AS Result;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.