Creazione di Custom TYPE: differenze tra le versioni
Da Webmobili Wiki.
Nessun oggetto della modifica |
|||
| Riga 31: | Riga 31: | ||
* Creare un DataTable con gli ID | * Creare un DataTable con gli ID | ||
* Passarlo come parametro alla stored procedure | * Passarlo come parametro alla stored procedure | ||
<syntaxhighlight lang="c#"> | |||
// Creare un DataTable per il TVP | |||
DataTable tvpTable = new DataTable(); | |||
tvpTable.Columns.Add("Id", typeof(int)); | |||
foreach (int id in ids) | |||
{ | |||
tvpTable.Rows.Add(id); | |||
} | |||
</syntaxhighlight > | |||
Versione delle 18:17, 18 feb 2025
Info
Se vogliamo passare a SQL Server un tipo complesso, ad esempio una lista bisogna usare le TVP (Table-Valued Parameter)
Per vedere tutti i TYPE presenti sul Database:
SELECT * FROM sys.types;
Creazione TYPE
Possiamo creare un tipo custom per una lista di interi come tabella:
CREATE TYPE IntList AS TABLE (Id INT);
Uso in SP
ALTER PROCEDURE [dbo].[UTILS_SPAM_remove_lead]
(
@repositoryIds IntList READONLY -- Usiamo il tipo creato sopra
)
AS
BEGIN
SET NOCOUNT ON;
-- Eliminare solo i record con repositoryID presenti nella lista
DELETE FROM Lead WHERE repositoryID IN (SELECT Id FROM @repositoryIds);
END
Uso in C#
- Creare un DataTable con gli ID
- Passarlo come parametro alla stored procedure
// Creare un DataTable per il TVP
DataTable tvpTable = new DataTable();
tvpTable.Columns.Add("Id", typeof(int));
foreach (int id in ids)
{
tvpTable.Rows.Add(id);
}