答案 0 :(得分:0)
我无法理解您的要求。但是您可以查询sys.columns以获取列名,以查找具有相同列的表
select
OBJECT_NAME(object_id) as table_name,
name as column_name
from sys.columns
where
name like N'%id%'
要将ID列表传递到存储过程中,最新方法是使用表值参数(又称为TVP)
以下是在SQL Server中使用TVP的示例
首先,您需要在数据库中创建类型。然后,用数据填充表类型的变量。您必须在前端进行这些操作。然后将此参数传递给存储过程
我使用数据库视图作为示例,您可以改用表和ID列
create type IDTableType as table
(
Id int
);
go
create procedure ReadData (
@IdList IDTableType readonly
)
as
select *
from sys.databases as d
inner join @IdList as p
on d.database_id = p.id
go
declare @IDs as IDTableType
insert into @IDs values (1),(5),(3)
exec ReadData @IDs
但是最常见的用法是使用字符串参数,该参数是ID的串联形式
这次,您需要使用SQL split string function将输入参数拆分为ID列表。您可以使用引用的样本,也可以使用Web上的自定义拆分功能之一。如果您使用的是SQL Server 2016或更高版本,则也可以使用string_split function
create procedure ReadData2 (
@IdList varchar(max)
)
as
select *
from sys.databases as d
inner join dbo.split(@IdList,0,0) as s
on d.database_id = s.val
go
declare @IDs as varchar(max) = '1,3,5,6'
exec ReadData2 @IDs
答案 1 :(得分:0)
我在sql中取笑的第一件事:
USE [ZAB_HCTMS]
GO
/****** Object: UserDefinedFunction [dbo].[LCNos] Script Date: 1/8/2019 1:09:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object: UserDefinedTableType [dbo].[oldTVP_ConsignmentNoteLineItems] Script Date: 1/7/2019 1:00:53 PM ******/
ALTER FUNCTION [dbo].[LCNos]
(
@LCNo NVARCHAR(MAX),
@Comma CHAR(1)
)
RETURNS @Output TABLE (
ID NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT
SET @StartIndex = 1
IF SUBSTRING(@LCNo, LEN(@LCNo) - 1, LEN(@LCNo)) <> @Comma
BEGIN
SET @LCNo = @LCNo + @Comma
END
WHILE CHARINDEX(@Comma, @LCNo) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Comma, @LCNo)
INSERT INTO @Output(ID)
SELECT SUBSTRING(@LCNo, @StartIndex, @EndIndex - 1)
SET @LCNo = SUBSTRING(@LCNo, @EndIndex + 1, LEN(@LCNo))
END
RETURN
END
然后为此:
-- EXEC GetLCsDetails '2188,2196,2201'
alter PROCEDURE GetLCsDetails
@LCNos VARCHAR(100)
AS
BEGIN
SELECT lr.*, lr.PayBalance 'TotalPayBalance', s.Name 'SuppName',
cb.AdvancePayment, s.[Address] 'Address',
s.PinCode, pod.SupplierBillNo
FROM LorryChallans lr
left join ConsignmentBookings cb on lr.ConsignmentBookingID =
cb.ConsignmentBookingID
left join Suppliers s on cb.VehicleSupplierID = s.SupplierID
left join POD pod on lr.ConsignmentBookingID = pod.ConsignmentBookingID
WHERE
lr.LorryChallanNumber
IN( SELECT CAST(ID AS INTEGER) FROM dbo.LCNos(@LCNos, ',') )
END
最后通过字符串逗号分隔jquery中的ID:
function chk()
{
$('.Checkbox:checked').map(function () {
return $(this).attr('data-lcno');
}).get().join(',')
}
data-lcno是复选框列表的数据属性。