TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Kumar AU
1.4k
309
61.9k
Generate DELETE Query using ONLY IN operator
Nov 2 2020 4:50 PM
I am trying to write a query that generates the Delete query automatically based on the requirements. I will get data from different tables and dump into temp table and then using this temp table I need to write query using IN operator, please help me
This is what I have tried
SET
NOCOUNT
ON
DECLARE
@DeleteDisbursement
TABLE
(
DisbursementId
BIGINT
,
PolicyNumber NVARCHAR(10),
DisbursementAmount
DECIMAL
(19, 4)
)
DECLARE
@DeleteRecords
Table
(DisbursementId
BIGINT
, ActivityId
BIGINT
)
INSERT
INTO
@DeleteDisbursement (DisbursementId, PolicyNumber, DisbursementAmount)
VALUES
(4576,
'12345'
, 3.00),
(1232,
'65455'
, 143.44),
(2341,
'34234'
, 228.95),
(1111,
'23155'
, 414.89)
SELECT
*
from
@DeleteDisbursement
DECLARE
@GenerateScriptCount
BIGINT
= 0
DECLARE
@DisbursementIDValue
BIGINT
= 0
DECLARE
@ActivityIdValue
BIGINT
= 0 WHILE EXISTS(
SELECT
Top
1 1
FROM
@DeleteDisbursement
)
BEGIN
DECLARE
@DisbursementId
BIGINT
= 0
DECLARE
@PolicyNumber NVARCHAR(10)
DECLARE
@AccountId
BIGINT
= 0
DECLARE
@ActivityId
BIGINT
DECLARE
@DisbursementAmount
DECIMAL
(19, 4)
SELECT
TOP
1 @DisbursementId = DisbursementId,
@PolicyNumber = PolicyNumber,
@DisbursementAmount = DisbursementAmount
FROM
@DeleteDisbursement
SET
@ActivityId = (
SELECT
ActivityId
FROM
ActivityLog
WHERE
ActivityAmount = @DisbursementAmount
) IF (@ActivityId > 0)
BEGIN
INSERT
INTO
@DeleteRecords (DisbursementId, ActivityId)
VALUES
(@DisbursementId, @ActivityId)
END
DELETE
FROM
@DeleteDisbursement
WHERE
DisbursementId = @DisbursementId
END
WHILE EXISTS(
SELECT
Top
1 1
FROM
@DeleteRecords
)
BEGIN
SELECT
TOP
1 @DisbursementIDValue = @DisbursementId,
@ActivityIdValue = @ActivityId
FROM
@DeleteRecords PRINT
'DELETE FROM Disbursement Where DisbursementId IN ('
+
CONVERT
(NVARCHAR(
MAX
), @DisbursementIDValue) +
')'
PRINT
'DELETE FROM ActivityLog WHERE ActivityId IN ('
+
CONVERT
(NVARCHAR(
MAX
), @ActivityIdValue) +
')'
DELETE
FROM
@DeleteRecords
WHERE
DisbursementId = @DisbursementId
END
SET
NOCOUNT
OFF
;
But its not working, Please help me How do I write query only 1 query which included in IN Operator like below :-
DELETE
FROM
Disbursement
Where
DisbursementId
IN
(4576, 1232, 2341, 1111)
DELETE
FROM
ActivityLog
WHERE
ActivityId
IN
(1000, 2000, 3000, 4000)
Reply
Answers (
5
)
Concatenate multiple XML rows to one in SQL table
When I run my mvc solution from another user it doesn’t connect to sql