简体   繁体   English

PHP MS SQL - 在存储过程中传递多值一个字段

[英]PHP MS SQL - Multiple value one field pass in a stored procedure

I am having a bit of trouble with MS SQL and PHP.我在使用 MS SQL 和 PHP 时遇到了一些麻烦。 Would you guide me with this?你会用这个来指导我吗?

Here is my story:这是我的故事:

I have a delivery report.我有交货报告。 The report have filters.报告有过滤器。 Take a look at my form:看看我的表格:

Delivery Report送货报告

Now the filters are working fine.现在过滤器工作正常。 I can retrieve data.我可以检索数据。 But my boss wants my filter to be multiple value per field.但是我的老板希望我的过滤器为每个字段设置多个值。

Example:例子:

In the Sales Group field, If I put "01, 07" all the delivery from sales group 01 and 07 would appear in the table.在销售组字段中,如果我输入“01, 07”,则来自销售组 01 和 07 的所有交货都将出现在表中。 Currently I am using LIKE condition.目前我正在使用 LIKE 条件。

Now my question is, how can I pass comma delimited values into my MS SQL stored procedure?现在我的问题是,如何将逗号分隔的值传递到我的 MS SQL 存储过程中?

Maybe my approach is wrong.也许我的方法是错误的。 Is there other way to do this?有没有其他方法可以做到这一点? Is it on MS Sql part or the PHP part?是在 MS Sql 部分还是 PHP 部分?

Here's my sql query: (UPdate: Here is the final QUERY that solved my problem. Thanks PSA )这是我的 sql 查询:(更新:这是解决我的问题的最终查询。谢谢 PSA

 -- inventoryDetailed '0', '10', '1', '', '', '', '02/01/2018', '02/28/2018' -- inventoryDetailed '0', '10', '1', NULL, NULL, NULL, '01/01/2018', '05/31/2018' -- exec inventoryDetailed 0, 10, 1, NULL, NULL, NULL, '01/01/2018', '07/28/2018' ALTER PROCEDURE [dbo].[inventoryDetailed] @start int, @pageSize int, @userId bigint, @salesGroupCode nvarchar(10), @salesOfficeCode nvarchar(10), @salesDistrictCode nvarchar(10), @DtRcvFrom DATETIME, @DtRcvTo DATETIME AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; DECLARE @storeSalesGroup nvarchar(max) DECLARE @salesGroupTable TABLE ( salesGroup nvarchar(10) ) SET @storeSalesGroup = @salesGroupCode while len(@salesGroupCode) > 0 begin --exec YourSP left(@S, charindex(',', @S+',')-1) insert into @salesGroupTable values(left(@salesGroupCode, charindex(',', @salesGroupCode +',')-1)) set @salesGroupCode = stuff(@salesGroupCode, 1, charindex(',', @salesGroupCode+','), '') end SELECT d.SGrp as salesGroupCode, d.SGrpNm as salesGroup, d.SOffc as salesOfficeCode, d.SOffcNm as salesOffice, d.SDst as salesDistrictCode, d.SDstNm as salesDistrict, a.CustCode as customerCode, d.CustNm as customer, c.MatrlCode as materialCode, c.MatDesc as material, b.Qty as qty, b.ExpDt as batch, datepart(wk, a.DtRcv) as week, a.DtRcv as dateRcvd, e.LName + ', ' + e.FName as diser, overall_count = COUNT(*) OVER() FROM [BigEMerchandiser].[dbo].[tbl_Inventory_H] as a INNER JOIN [BigEMerchandiser].[dbo].[tbl_Inventory_D] as b ON a.TransCtr = b.TransCtr INNER JOIN BigEMasterData.dbo.tbl_Materials as c ON b.Material = c.ExtMatGrp INNER JOIN BigEMasterData.dbo.tbl_Customers as d ON a.CustCode = d.CustCode inner JOIN [BigESentData].[dbo].[tbl_sentRegistration] as e on a.CreatedBy = e.CellNum inner join BigEUsers.dbo.user_role_area as f on d.SDst = f.area_id inner join BigEUsers.dbo.users as g on f.role_id = g.role_id WHERE datepart(wk, a.DtRcv) BETWEEN datepart(wk, @DtRcvFrom) AND datepart(wk, @DtRcvTo) AND (d.SGrp IN (SELECT salesGroup from @salesGroupTable)) OR (ISNULL(@storeSalesGroup,'')='') AND d.SOffc LIKE ISNULL('%' + @salesOfficeCode + '%', d.SOffc) AND d.SDst LIKE ISNULL('%' + @salesDistrictCode + '%', d.SDst) AND g.id = @userId ORDER BY a.DtRcv desc OFFSET @start ROWS FETCH NEXT @pageSize ROWS ONLY; END

Any tips and suggestion would be much appreciated.任何提示和建议将不胜感激。 Thankyou so much.非常感谢。

EDIT:编辑:

Thanks PSK for the idea.感谢 PSK 的想法。

What I did is pass the string from PHP.我所做的是从 PHP 传递字符串。 Example: '01, 07'.示例:'01, 07'。

Then, in MS SQL, I created a table variable.然后,在 MS SQL 中,我创建了一个表变量。

DECLARE @salesGroupTable TABLE
(
  salesGroup nvarchar(10)
)

Next, I will get split the string passed from PHP ('01, 07') and then insert it to the table variable.接下来,我将拆分从 PHP ('01, 07') 传递的字符串,然后将其插入到表变量中。

Next, I will use that table variable in my IN condition.接下来,我将在 IN 条件中使用该表变量。

WHERE  d.SGrp IN (SELECT salesGroup from @salesGroupTable)

Now, it is working fine.现在,它运行良好。 It is working as expected.它按预期工作。

But if the user didn't input anything, I must return all the results.但是如果用户没有输入任何内容,我必须返回所有结果。 Hope you can help me with this.希望你能帮我解决这个问题。 Thanks!谢谢!

Unfortunately Table-valued parameters are not yet supported by the PHP MSSQL Driver .不幸的是, PHP MSSQL Driver尚不支持表值参数。 So you have to live with passing a comma separated string to the stored procedure from your PHP code.因此,您必须忍受将逗号分隔的字符串从 PHP 代码传递到存储过程。

More details on data type supported by PHP MSSQL Driver有关PHP MSSQL 驱动程序支持的数据类型的更多详细信息

Use inbuilt function STRING_SPLIT to get the desired behavior.使用内置函数 STRING_SPLIT 来获得所需的行为。

You can implement your code like following.您可以像下面这样实现您的代码。

DECLARE @Codes NVARCHAR(400) = 'Code1,Code2,Code3,Code4,Code5'  

SELECT * FROM [Your_Table] UT
INNER JOIN
(
SELECT IN_Code  
FROM STRING_SPLIT(@Codes, ',')  

) INPUT_DATA
ON INPUT_DATA.IN_Code = UT.Code

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

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