Deklarasikan daftar variabel dalam prosedur tersimpan SQL Server

Saya ingin menghapus data dari beberapa tabel dengan kondisi yang sama (klausa mana) untuk setiap pernyataan penghapusan.

delete from tblA where id in (select x.id from tblX x where name like N'%test%')
delete from tblB where id in (select x.id from tblX x where name like N'%test%')
delete from tblC where id in (select x.id from tblX x where name like N'%test%')
delete from tblD where id in (select x.id from tblX x where name like N'%test%')

Apakah ada cara untuk mendeklarasikan daftar yang menyimpan id dari pernyataan pilih di atas?

Saya mencoba:

declare @ids int
set @ids = select x.id from tblX x where name like N'%test%'

Tapi ia mengeluhkan hal itu

Subquery mengembalikan lebih dari 1 nilai. Hal ini tidak diperbolehkan bila subkueri mengikuti =, !=, ‹, ‹= , >, >= atau bila subkueri digunakan sebagai ekspresi.

Mohon sarannya, terima kasih.


person Eddie    schedule 22.02.2013    source sumber
comment
Pikirkan mengapa top 1 membuat ini berhasil. stackoverflow.com/ pertanyaan/11232751/   -  person Tim    schedule 22.02.2013


Jawaban (3)


Anda tetap memerlukan tabel, tetapi setidaknya Anda menghindari banyak pemrosesan dengan melakukan hal seperti setiap kali:

-- create a table variable
declare @ids table
(
  id int not null
)

-- insert the id into the table variable
insert into @ids
select id from table1 where column1 like '%something%'

-- delete
delete from tablen where id in (select * from @ids)

Anda juga dapat menggunakan tabel sementara, tampilannya sama, tetapi alih-alih @ids, Anda memerlukan #ids, dan Anda harus menghapus tabel sementara setelah pekerjaan selesai.

Untuk memilih antara tabel sementara (tabel fisik) atau variabel tabel (tabel seperti memori), Anda benar-benar perlu melakukan beberapa pengujian, tetapi menurut definisi, data kompleks berfungsi lebih baik di tabel sementara. Jika Anda hanya perlu menyimpan sejumlah kecil id untuk waktu yang singkat, saya yakin variabel tabel lebih baik.

Apa perbedaan antara temp tabel dan variabel tabel di SQL Server?

person lolol    schedule 22.02.2013
comment
Variabel tabel bukan hanya memori. Itu adalah mitos. Untuk perbedaan non mitos lihat jawaban saya di sini - person Martin Smith; 23.02.2013
comment
Saya tidak bermaksud demikian, dan juga memposting tautan ke perbedaannya. - person lolol; 25.02.2013

Anda dapat mendeklarasikan tabel sementara dan kemudian memilih id yang akan Anda hapus ke dalam tabel itu.

CREATE TABLE #IDS_from_tblA (id int)
INSERT INTO #IDS_from_tblA(id)
    SELECT x.id FROM tblA WHERE x.id in (select x.id from tblX x where name like N'%test%')
delete from tblA where id in (select x.id from tblX x where name like N'%test%')

(Do whatever you want to do with the ids)

DROP TABLE #IDS_from_tblA 
person Narnian    schedule 22.02.2013

Di server sql 2008+

declare @IdList table (Id int primary key)

insert into @IDList (Id)
select x.id from tblX x where name like N'%test%'

delete from tblA where id in (select x.id from @IDList x)

Jika Anda memiliki lebih dari ratusan catatan, Anda dapat menggunakan tabel temp alih-alih variabel tabel.

person Morzel    schedule 22.02.2013