我有一个表A,其中包含列“ ColumnName”(它将包含逗号分隔的整数值),并且我有一个存储过程,该过程采用的参数也是逗号分隔的整数值。 例如,我在表“ 101,102,103”和“ 103,104,105”中有值,并且用户输入为“ 101,104”,它应该返回2条记录。我该如何实现? 需要SQL语句
答案 0 :(得分:0)
如果您需要帮助,说明,请告诉我:)
DECLARE
@input VARCHAR(MAX) = '101,104',
@separator CHAR(1) =',',
@separatorPosition INT,
@value VARCHAR(MAX),
@start INT = 1
DECLARE @split_input TABLE(Value VARCHAR(MAX))
DECLARE @tmp TABLE (st VARCHAR(50))
INSERT INTO @tmp VALUES ('101,102,103')
INSERT INTO @tmp VALUES ('103,104,105')
INSERT INTO @tmp VALUES ('106,107,108')
SET @separatorPosition = CHARINDEX(@separator, @input)
IF @separatorPosition = 0
BEGIN
INSERT INTO @split_input
VALUES
(
@input
)
RETURN
END
SET @input = @input + @separator
WHILE @separatorPosition > 0
BEGIN
SET @value = SUBSTRING(@input, @start, @separatorPosition - @start)
IF (@value <> '')
INSERT INTO @split_input
VALUES
(
@value
)
SET @start = @separatorPosition + 1
SET @separatorPosition = CHARINDEX(@separator, @input, @start)
END
SELECT tmp.st FROM @tmp AS tmp INNER JOIN @split_input AS split ON tmp.st LIKE '%' + split.Value + '%'
答案 1 :(得分:0)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[fnParseArray] (@Array VARCHAR(MAX),@separator CHAR(1))
RETURNS @T Table (col1 varchar(50))
AS
BEGIN
-- @Array is the array we wish to parse
-- @Separator is the separator charactor such as a comma
DECLARE @separator_position INT -- This is used to locate each separator character
DECLARE @array_value VARCHAR(MAX) -- this holds each array value as it is returned
-- For my loop to work I need an extra separator at the end. I always look to the
-- left of the separator character for each array value
SET @array = @array + @separator
-- Loop through the string searching for separtor characters
WHILE PATINDEX('%' + @separator + '%', @array) <> 0
BEGIN
-- patindex matches the a pattern against a string
SELECT @separator_position = PATINDEX('%' + @separator + '%',@array)
SELECT @array_value = LEFT(@array, @separator_position - 1)
-- This is where you process the values passed.
INSERT into @T VALUES (@array_value)
-- Replace this select statement with your processing
-- @array_value holds the value of this element of the array
-- This replaces what we just processed with and empty string
SELECT @array = STUFF(@array, 1, @separator_position, '')
END
RETURN
END
select * from [dbo].[fnParseArray]('a,b,c,d',',')
答案 2 :(得分:0)
这应该做到:
val accessLog = Logging(system.eventStream, "access_log")
val routes =
path("ping" ) {
withLog(accessLog) {
logRequest("ping", Logging.InfoLevel) {
complete("pong")
}
}
}
您可以像这样测试存储过程:
val loggingAdapter : LoggingAdapter = new LoggingAdapter {
override def isDebugEnabled : Boolean = logger.isDebugEnabled
override def isErrorEnabled : Boolean = logger.isErrorEnabled
override def isInfoEnabled : Boolean = logger.isInfoEnabled
override def isWarningEnabled : Boolean = logger.isWarnEnabled
override def notifyDebug(message: String): Unit = logger.debug(message)
override protected def notifyError(message: String): Unit = logger.error(message)
override protected def notifyError(cause: Throwable, message: String): Unit = logger.error(message, cause)
override protected def notifyWarning(message: String): Unit = logger.warn(message)
override protected def notifyInfo(message: String): Unit = logger.info(message)
}
val routes =
path("ping" ) {
withLog(loggingAdapter) {
logRequest("**ping**", Logging.InfoLevel) {
complete("pong")
}
}
}