问题
Using, SQL Server 2012, I would like to create a stored procedure, that passes in a list of string and checks it for each entry.Iv added the list to one comma separated string 'UserGroupsAllowedToViewMap'. This was working for one entry but I need to check it for a number of entries.
public DataTable GetMapsWithWorkspaceForUserGroups(int workspaceID, string UserGroupsAllowedToViewMap)
{
DataTable mapDets = new DataTable();
SqlCommand oComm = new SqlCommand();
SqlParameter spParam_WrkSpaceId = new SqlParameter();
SqlParameter spParam_ViewMap = new SqlParameter();
SqlParameter[] spParams = new SqlParameter[2];
SqlDataAdapter daUserMaps = new SqlDataAdapter();
try
{
spParam_WrkSpaceId.ParameterName = "@workspaceID";
spParam_WrkSpaceId.Value = workspaceID;
spParams[0] = spParam_WrkSpaceId;
spParam_ViewMap.ParameterName = "@ViewMap";
spParam_ViewMap.Value = UserGroupsAllowedToViewMap;
spParams[1] = spParam_ViewMap;
oComm = CreateCommand("GetWorkspaceMapDetailsForUserByGroups", spParams, TypeOfConnectionString.GeoAppBuilder);
daUserMaps.SelectCommand = oComm;
daUserMaps.Fill(mapDets);
}
catch (Exception e)
{
throw (e);
}
finally
{
CloseConnection();
}
return mapDets;
}
USE [App]
GO
/****** Object: StoredProcedure [dbo].[GetWorkspaceMapDetailsForUserByGroups] Script Date: 16/02/2015 10:37:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetWorkspaceMapDetailsForUserByGroups]
@workspaceID int,
@viewMap nvarchar(256)
AS
SELECT
m.*
FROM
GeoAppMapDef m
WHERE
m.workspaceID = @workspaceID
and m.IsDeleted = 0
and m.ViewMap = @viewMap
I am unsure how to iterate through the string in SQL. Ive looked at Passing List<> to SQL Stored Procedure and C# SQL Server - Passing a list to a stored procedure but still none the wiser. any help appreciated.
回答1:
Convert the comma seperated value to table using the XML. Use this updated procedure.
USE [App]
GO
/****** Object: StoredProcedure [dbo].[GetWorkspaceMapDetailsForUserByGroups]
Script Date: 16/02/2015 10:37:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetWorkspaceMapDetailsForUserByGroups]
@workspaceID int,
@viewMap nvarchar(256)
AS
SELECT
m.*
FROM
GeoAppMapDef m
WHERE
m.workspaceID = @workspaceID
and m.IsDeleted = 0
and m.ViewMap IN
(
SELECT
Split.a.value('.', 'VARCHAR(100)') AS CVS
FROM
(
SELECT CAST ('<M>' + REPLACE(@viewMap, ',', '</M><M>') + '</M>' AS XML) AS CVS
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
)
回答2:
This is really a duplicate of the links you posted. Instead of trying to parse a list of values, pass a table-valued parameter.
First create the parameter's type in the database (only once).
CREATE TYPE [dbo].[IdList] AS TABLE(
[Id] int NULL
);
Then create a procedure that accepts this parameter:
CREATE PROCEDURE [dbo].[GetWorkspaceMapDetailsForUserByGroups]
@workspaceID int,
@groupIds IdList READONLY
AS
BEGIN
SELECT
m.*
FROM GeoAppMapDef m
inner join @groupIds on m.ViewMap=@groupIds.Id
WHERE
m.workspaceID = @workspaceID
and m.IsDeleted = 0
END
On the client's side, create a DataTable with a single int-typed column called Id, fill it with the IDs you want then use it as the value of the @groupIds parameter
var table = new DataTable();
table.Columns.Add("Id", typeof(int));
for (int i = 0; i < 10; i++)
table.Rows.Add(i);
var pList = new SqlParameter("@groupIds", SqlDbType.Structured);
pList.TypeName = "dbo.IdList";
pList.Value = table;
I've copied this from the duplicate question with a few modifications.
来源:https://stackoverflow.com/questions/28541079/pass-list-of-strings-to-a-stored-procedure