当分配的值是静态时,它正在工作
DECLARE @str varchar(MAX)='45 | 00055 | 9/30/2016 | Vodafone | Randy Singh |Newyork | Test Msg | TBL101 | PC | 1.00 | COMP101 | CS | 1.00'
SELECT * FROM dbo.SUBSTRING_INDEX (@str,'|',7) (Function)
无效
DECLARE @str VARCHAR(1000)
SELECT @Str = [temp] FROM TempTable
它只从表中选择最后一行值,我需要传递多个值
我应该如何将@str分配给sql表结果值。 Temp(列有很多行)。
功能代码。
alter FUNCTION dbo.SUBSTRING_INDEX_1
(
@str NVARCHAR(4000),
@delim NVARCHAR(1),
@count INT
)RETURNS @rtnTable TABLE
(
HeaderData NVARCHAR(2000),
DetailData NVARCHAR(2000)
)
AS
BEGIN
DECLARE @cnt INT=1;
DECLARE @subStringPoint INT = 0
WHILE @cnt <=@count
BEGIN
SET @subStringPoint=CHARINDEX(@delim,@str,@subStringPoint)+1
SET @cnt=@cnt+1
END
INSERT INTO @rtnTable
SELECT SUBSTRING(@str,0,@subStringPoint-1) ,SUBSTRING(@str,@subStringPoint+1,LEN(@str))
RETURN
END
它正在运作
DECLARE @str varchar(MAX)='45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg | TBL101 | PC | 1.00 | COMP101 | CS | 1.00'
SELECT * FROM dbo.SUBSTRING_INDEX_1 (@str,'|',7)
无效
DECLARE @str TABLE (ObjectNames VARCHAR)
INSERT INTO @str
SELECT o.temp FROM OMSOrderTemp o
SELECT * FROM @str
SELECT * FROM dbo.SUBSTRING_INDEX_1(@str, N'|', 7)
答案 0 :(得分:0)
您需要使用表值参数...
DECLARE @str TABLE(temp VARCHAR(max))
INSERT INTO @str
SELECT [temp] FROM TempTable
SELECT * FROM @str
以下是测试数据的示例,显示了使用sys.objects ...
的所有对象的名称DECLARE @str TABLE (ObjectNames VARCHAR(4000))
INSERT INTO @str
SELECT o.Name FROM sys.objects o
SELECT * FROM @str