Benutzer mit den meisten Antworten
StoredProcedure Parameter für WHERE IN

Frage
-
Antworten
-
Hallo Andrej,
hier ist ein Beispiel, wie man dafür einen user-defined table type einsetzen könnte
Schau Dir noch den Link Table-Valued Parameters (Database Engine) an.
use tempdb go create table MyTable ( MyTableID int identity, Col1 nvarchar(10) ) go insert into MyTable(Col1) values ('text1'),('text2'),('text3') go create type MyType as table ( MyTypeID int identity, Col2 nvarchar(10) ) go create proc MyProc @myType MyType readonly as begin select * from MyTable where MyTableID in ( select MyTypeID from @myType ) end go declare @myType MyType insert into @myType values ('text4'),('text5') exec dbo.MyProc @myType go drop table dbo.MyTable go drop proc dbo.MyProc go drop type dbo.MyType go
- Als Antwort markiert andrej75 Freitag, 13. Mai 2011 05:39
-
Als Alternative zu Yury's Antwort, kannst Du auch den die Liste als String übergeben, diesen in einer Table-Valued Split Funktion zerlegen und dann darauf joinen. Also, etwa in der Art:
<pseudocode> SELECT whatever... FROM Table T JOIN FunkyHelperFunction(@CSV) F ON T.SomeKey = F.SomeKey </pseudocode>
Das ist dann identisch zuSELECT whatever... FROM Table T WHERE T.somekey IN (@CSV)
-- Frank Kalis Microsoft SQL Server MVP Webmaster: http://www.insidesql.org- Bearbeitet Frank Kalis Freitag, 13. Mai 2011 04:43 Formatierung
- Als Antwort markiert andrej75 Freitag, 13. Mai 2011 05:39
Alle Antworten
-
Hallo Andrej,
hier ist ein Beispiel, wie man dafür einen user-defined table type einsetzen könnte
Schau Dir noch den Link Table-Valued Parameters (Database Engine) an.
use tempdb go create table MyTable ( MyTableID int identity, Col1 nvarchar(10) ) go insert into MyTable(Col1) values ('text1'),('text2'),('text3') go create type MyType as table ( MyTypeID int identity, Col2 nvarchar(10) ) go create proc MyProc @myType MyType readonly as begin select * from MyTable where MyTableID in ( select MyTypeID from @myType ) end go declare @myType MyType insert into @myType values ('text4'),('text5') exec dbo.MyProc @myType go drop table dbo.MyTable go drop proc dbo.MyProc go drop type dbo.MyType go
- Als Antwort markiert andrej75 Freitag, 13. Mai 2011 05:39
-
Als Alternative zu Yury's Antwort, kannst Du auch den die Liste als String übergeben, diesen in einer Table-Valued Split Funktion zerlegen und dann darauf joinen. Also, etwa in der Art:
<pseudocode> SELECT whatever... FROM Table T JOIN FunkyHelperFunction(@CSV) F ON T.SomeKey = F.SomeKey </pseudocode>
Das ist dann identisch zuSELECT whatever... FROM Table T WHERE T.somekey IN (@CSV)
-- Frank Kalis Microsoft SQL Server MVP Webmaster: http://www.insidesql.org- Bearbeitet Frank Kalis Freitag, 13. Mai 2011 04:43 Formatierung
- Als Antwort markiert andrej75 Freitag, 13. Mai 2011 05:39