在 SELECT 语句中使用存储过程或函数
我有几个关于如何使用此 SELECT 语句执行我想要的操作的问题,但主要问题是如何在 SELECT 语句中使用函数/存储过程。
我有类似下面例子的东西。
虽然移动位足够小,可以工作,但属性(力量、护甲、意志、敏捷)很复杂,因为数字字母组合出现的顺序没有限制,并且需要用于 4 个不同的字段。编写一个过程并重复使用 4 次而不是重复代码是有意义的。
我曾尝试编写一个用于移动的程序,但除了 EXEC testProcedure 之外似乎无法执行任何操作,甚至 PRINT( testProcedure 4 ) 似乎也不起作用。
有没有办法在查询字段上使用用户定义的函数/过程?
SELECT DISTINCT
Main.CardName, Main.CardType, Main.ProductSet, Main.Movement, Main.Actions,
Main.Strength, Main.Armor, Main.Will, Main.Dexterity, Main.Health,
Main.Potions,
(
SELECT COUNT( DISTINCT KeywordName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex
) AS KeywordCount,
(
SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Action'
) AS ActionCount,
(
SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Potion'
) AS SkillCount,
(
--Movement
(
SELECT CASE
WHEN Main.Movement = 5 THEN 1
WHEN Main.Movement = 6 THEN 1
WHEN Main.Movement = 7 THEN 3
WHEN Main.Movement = 8 THEN 3
WHEN Main.Movement = 9 THEN 6
WHEN Main.Movement = 10 THEN 6
ELSE 0
END
)
--Actions
+(Main.Actions),
--STR
+(
SELECT CASE
WHEN Main.Strength = 5 THEN 1
ELSE 0
END
)
--ARM
+(SELECT CASE
WHEN Main.Armor = 5 THEN 1
ELSE 0
END)
--WILL
+(SELECT CASE
WHEN Main.Will = 5 THEN 1
ELSE 0
END)
--DEX
+(SELECT CASE
WHEN Main.Dexterity = 5 THEN 1
ELSE 0
END)
--Health
+(Main.Health)
--Potions
+(Main.Potions)
--KeywordCount
+(SELECT COUNT( DISTINCT KeywordName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex)
--ActionCount
+(SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Action')
--SkillCount
+(SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Potion' )
) AS BONUSES
FROM HeroesViewMultiLine Main
I have a couple of questions about how to do what I want with this SELECT
statement, but the main thing is how to use a function / stored procedure in the SELECT
statement.
I have something like the example below.
While the Movement bit is small enough to work, the attributes (Strength, Armor, Will, Dexterity) are complex as there aren't restrictions on the order in which number-letter combinations can occur and it needs to be used for 4 different fields. It makes sense to write a procedure and reuse that 4 times instead of repeating code.
I had attempted to write one for movement, but can't seem to do anything other than EXEC testProcedure
, even PRINT( testProcedure 4 )
doesn't seem to work.
Is there a way use a user defined function / procedure on queried field?
SELECT DISTINCT
Main.CardName, Main.CardType, Main.ProductSet, Main.Movement, Main.Actions,
Main.Strength, Main.Armor, Main.Will, Main.Dexterity, Main.Health,
Main.Potions,
(
SELECT COUNT( DISTINCT KeywordName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex
) AS KeywordCount,
(
SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Action'
) AS ActionCount,
(
SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Potion'
) AS SkillCount,
(
--Movement
(
SELECT CASE
WHEN Main.Movement = 5 THEN 1
WHEN Main.Movement = 6 THEN 1
WHEN Main.Movement = 7 THEN 3
WHEN Main.Movement = 8 THEN 3
WHEN Main.Movement = 9 THEN 6
WHEN Main.Movement = 10 THEN 6
ELSE 0
END
)
--Actions
+(Main.Actions),
--STR
+(
SELECT CASE
WHEN Main.Strength = 5 THEN 1
ELSE 0
END
)
--ARM
+(SELECT CASE
WHEN Main.Armor = 5 THEN 1
ELSE 0
END)
--WILL
+(SELECT CASE
WHEN Main.Will = 5 THEN 1
ELSE 0
END)
--DEX
+(SELECT CASE
WHEN Main.Dexterity = 5 THEN 1
ELSE 0
END)
--Health
+(Main.Health)
--Potions
+(Main.Potions)
--KeywordCount
+(SELECT COUNT( DISTINCT KeywordName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex)
--ActionCount
+(SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Action')
--SkillCount
+(SELECT COUNT( DISTINCT AbilityName )
FROM HeroesViewMultiLine
WHERE CardIndex = Main.CardIndex and AbilityResource = 'Potion' )
) AS BONUSES
FROM HeroesViewMultiLine Main
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有许多不同的方法可以在Select语句中获取存储过程的结果,例如创建临时表,创建表变量,使用函数等等。但是,这是在Select语句中运行存储过程的最简单方法。
例如,这是您要执行的SP:
您可以在OpenRowset的帮助下运行以下选择语句并执行存储过程。
是的,确实很容易。当您使用OpenRowset运行Select语句时,如果您获得有关临时查询的错误,则可以运行以下命令来修复错误。
There are many different methods to get Stored Procedure’s results in the SELECT statement, such as creating a temp table, creating a table variable, using functions, and many more. However, here is the easiest method to run the stored procedure in SELECT statement.
For example here is the SP which you want to execute:
You can run the following SELECT statement with the help of OPENROWSET and execute the stored procedure.
Yes, indeed it is that easy. When you run the SELECT statement with OPENROWSET and if you get an error about ad hoc queries, you can run the following command to fix the error.