简体   繁体   English

在SQL变量中存储多个变量,并将其用于删除表中的条目

[英]Store multiple variables in SQL variable and use it for deleting entries in a table

Hi i was wondering how could a deletetion of multiple values can be achieved in a table. 您好我想知道如何在一个表中删除多个值。

I have a following query: 我有以下查询:

DELETE FROM dbo.MainJobs WHERE  @MainJobId =IDKey
declare @subjob nvarchar;
SET @subjob = (SELECT SubJobFamily FROM dbo.Jobz where  JobFamily = @MainJobId ) --has mutiple values in the table
Delete dbo.Jobz where JobFamily = @MainJobId
DELETE dbo.Relationship1 where SubJobs =  @subjob  --deletion of multiple rows

To summarize i am trying to delete the relationship of multiple sub jobs however there are multiple sub jobs that are linked to the main job family and I am getting an error while storing it into the variable - @subjob . 总而言之,我试图删除多个子作业的关系,但是有多个子作业链接到主作业系列,并且在将其存储到变量-@ subjob中时出现错误。 ANy help would be appreciated. 任何帮助,将不胜感激。

You can not store multiple values to a scalar variable, you need to have a table type to store the multiple values. 您不能将多个值存储到标量变量,需要具有表类型来存储多个值。

You can try like following using a temporary table. 您可以尝试使用临时表进行跟踪。

DECLARE @subjob NVARCHAR(20); 

SELECT subjobfamily 
INTO   #t 
FROM   dbo.jobz 
WHERE  jobfamily = @MainJobId 

DELETE dbo.jobz 
WHERE  jobfamily = @MainJobId 

DELETE dbo.relationship1 
WHERE  subjobs IN (SELECT subjobfamily 
                   FROM   #t) 
drop table #t

Also you need to specify the length of your @subjob variable, if you don't specify the length, it will just take the first character only. 另外,您需要指定@subjob变量的长度,如果不指定长度,它将仅使用第一个字符。

Change following declaration 更改以下声明

declare @subjob nvarchar;

to something like

declare @subjob nvarchar(100)

There are two types of variables in SQL Server - there are scalar variables (all built in data types are scalar variables) and there are table variables. SQL Server中有两种类型的变量-有标量变量(所有内置数据类型都是标量变量)和表变量。 You can declaring a table variable either by using a user-defined table type as it's type, or by specifying it as a table with all it's columns. 您可以通过使用用户定义的表类型作为表变量,也可以通过将其指定为包含所有列的表来声明表变量。

Suppose you have a UDT like this: 假设您有一个这样的UDT:

create type dbo.intList as table
(
    val int 
) 

you can declare a variable of that type, and it will be a table variable containing a single int column called val: 您可以声明该类型的变量,它将是一个表变量,其中包含一个称为val的int列:

declare @MyIntsTable As dbo.intList

Or you can declare a table variable like this: 或者,您可以像这样声明一个表变量:

declare @MyIntsTable as table
(
    val int 
)

However, I don't think you need to use a table variable in this situation. 但是,在这种情况下,我认为您不需要使用表变量。 Seems to me like you need something like this: 在我看来,您需要这样的东西:

BEGIN TRANSACTION

    BEGIN TRY

        DELETE 
        FROM dbo.MainJobs 
        WHERE @MainJobId =IDKey

        DELETE dbo.Relationship1 
        WHERE SubJobs IN
        (
            SELECT SubJobFamily 
            FROM dbo.Jobz 
            WHERE  JobFamily = @MainJobId
        )

        DELETE
        FROM dbo.Jobz 
        WHERE JobFamily = @MainJobId

    END TRY
    BEGIN CATCH
        IF @@TRANCOUNT > 0
            ROLLBACK TRANSACTION
    END CATCH

IF @@TRANCOUNT > 0
    ROLLBACK TRANSACTION

I've wrapped the delete statements in a transaction because I'm assuming you need all of them to succeed, but if one fails you want all to fail. 我将delete语句包装在一个事务中,因为我假设您需要所有语句才能成功,但是如果失败,则希望所有语句都失败。

This would be done in 2 step process. 这将在两步过程中完成。

First create a set of your required ID in comma separated string, then on the basis of each value in string delete your whole record from reference tables. 首先用逗号分隔的字符串创建一组所需的ID,然后根据字符串中的每个值从引用表中删除整个记录。

For creating comma separated string you can use 'stuff' or 'coalesce' . 要创建逗号分隔的字符串,可以使用'stuff''coalesce'

    DELETE FROM dbo.MainJobs WHERE  @MainJobId =IDKey
    declare @subjob nvarchar;
    SET @subjob = (SELECT SubJobFamily FROM dbo.Jobz where  JobFamily = @MainJobId ) -- use stuff or coalesce to get your result in comma separated string then use t-sql to delete your record
    Delete dbo.Jobz where JobFamily = @MainJobId
    DELETE dbo.Relationship1 where SubJobs =  @subjob  -- use t-sql over there

Or 要么

Another option is use cursor to fetch each record in loop and perform your required action from that record. 另一个选择是使用游标以循环方式获取每个记录并从该记录执行所需的操作。

    DELETE FROM dbo.MainJobs WHERE  @MainJobId =IDKey
    declare @subjob nvarchar;
    ----- use cursor over there to get each record one by one on SELECT SubJobFamily FROM dbo.Jobz where  JobFamily = @MainJobId
    SET @subjob = (SELECT SubJobFamily FROM dbo.Jobz where  JobFamily = @MainJobId ) --has mutiple values in the table
    Delete dbo.Jobz where JobFamily = @MainJobId
    DELETE dbo.Relationship1 where SubJobs =  @subjob  --deletion of multiple rows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM