我正在尝试为获取好友列表实现一些代码。 第一:
- I have my String id. E.G: 784717
- I have an string with 100 numbers. E.G: 7781,5913,551949194,4919491,...,444131 (One string separated by ,)
- I have 3000 records in my Database with different numbers. (With numbers I mean some kind of ID)
- Of my 100 numbers only 8 are registered in my database.
问题:
如何知道在数据库中注册了哪些数字并在其他表中插入关系? 我的表关系有这个列:
* number1 - (这里应该是我的身份证)
* number2 - (存在的100个数字中的1个)
所以在我的表中,Relationship应该是8个新行。
我尝试过:
EXEC('SELECT * FROM Accounts WHERE ID IN(' +@in_mystring+')')
但我不知道如何在另一张表中插入或者是否有效
答案 0 :(得分:0)
假设这是SQL Server,并借助解析器函数
例如
Select * from [dbo].[udf-Str-Parse]('7781,5913,551949194,4919491,...,444131',',')
返回
Key_PS Key_Value
1 7781
2 5913
3 551949194
4 4919491
5 ...
6 444131
通过此子查询,您可以将结果加入“帐户表”
也许是这样的
Select A.*
From Accounts A
Join (Select * from [dbo].[udf-Str-Parse]('7781,5913,551949194,4919491,...,444131',',')) B
on A.Key_Value =A.ID
UDF - 如果2016年,有一个原生解析器。
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@delimeter varchar(10))
--Usage: Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
-- Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
-- Select * from [dbo].[udf-Str-Parse]('id26,id46|id658,id967','|')
Returns @ReturnTable Table (Key_PS int IDENTITY(1,1) NOT NULL , Key_Value varchar(max))
As
Begin
Declare @intPos int,@SubStr varchar(max)
Set @IntPos = CharIndex(@delimeter, @String)
Set @String = Replace(@String,@delimeter+@delimeter,@delimeter)
While @IntPos > 0
Begin
Set @SubStr = Substring(@String, 0, @IntPos)
Insert into @ReturnTable (Key_Value) values (@SubStr)
Set @String = Replace(@String, @SubStr + @delimeter, '')
Set @IntPos = CharIndex(@delimeter, @String)
End
Insert into @ReturnTable (Key_Value) values (@String)
Return
End