Jef*_*eff 32 sql sql-server user-defined-functions
我从未使用过数据库函数,但我当前的项目需要它.我需要将一个常见的sql查询放入一个函数中,这样我们就不必在代码中输入数百次了.我已经创建了这个函数,但我不知道如何使用它.
这是功能代码:
USE [DB_NAME] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [dbo].[fn_GetConfigurationByProfile] ( @profileName AS NVARCHAR(50) ) RETURNS TABLE AS RETURN ( -- Fill the table variable with the rows for your result set SELECT system_settings_groups.ssg_id, system_settings_groups.ssg_name, system_settings_groups.ssg_parent_group_id, system_settings_names.ssn_name, system_Settings_names.ssn_display_name, system_settings_values.ssv_value FROM system_settings_profiles JOIN system_settings_values ON system_settings_profiles.ssp_id = system_settings_values.ssv_ssp_id JOIN system_settings_names ON system_settings_names.ssn_id = system_settings_values.ssv_ssn_id JOIN system_settings_groups ON system_settings_groups.ssg_id = system_settings_names.ssn_ssg_id WHERE system_settings_profiles.ssp_name = @profileName )
那我怎么在sql查询中使用它呢?我只使用SELECT fn_GetConfigurationByProfile('DEFAULTPROFILE')吗?
这可能是一个业余问题,但是哦.我需要帮助 :)
ani*_*key 43
你想使用FROM
例如:
select ...
FROM fn_GetConfigurationByProfile('DEFAULTPROFILE')
Run Code Online (Sandbox Code Playgroud)
试试这个
SELECT * FROM dbo.fn_GetConfigurationByProfile('DEFAULTPROFILE')
Run Code Online (Sandbox Code Playgroud)