views:

357

answers:

5

I've never worked with Database Functions, but my current project requires it. I need to put a common sql query into a function so we don't have to type it out in our code hundreds of times. I've got the function created, but I don't know how to use it.

Here's the function code:

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
    )

So how would I use this in a sql query? Do I just use SELECT fn_GetConfigurationByProfile('DEFAULTPROFILE')?

This may be an amateur question, but oh well. I need help :)

+1  A: 
SELECT *
FROM dbo.fn_GetConfigurationByProfile('DEFAULTPROFILE')
LukeH
A: 

You use it in the FROM clause, eg :

SELECT ....
FROM dbo.fn_GetConfigurationByProfile('DEFAULTPROFILE')

You can also join it to tables or use a where clause against it, among other things.

CodeByMoonlight
+1  A: 

try this

SELECT * FROM dbo.fn_GetConfigurationByProfile('DEFAULTPROFILE')
devio
+1  A: 

you want to use FROM

E.g :

select ...

    FROM fn_GetConfigurationByProfile('DEFAULTPROFILE')

SQL Server User-defined Functions

anishmarokey
Thanks for the timely response. I feel like a total noob to not have though of this since it was a table-valued function :/
Jeff
read the like that i given.hope that will help you more :)
anishmarokey
A: 

Others have shown how you can call your Table function within a standard query. However, can I suggest that you may prefer to create a View rather than a function?

CREATE VIEW [dbo].[ConfigurationView] AS
SELECT  
    system_settings_profiles.ssp_name,
    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

GO

Then you can use it in your SQL like this.

SELECT
    *
FROM
    ConfigurationView
WHERE
    ConfigurationView.ssp_name = 'DEFAULTPROFILE'

You will have the added options of indexing the view and also filtering on other data easily should you require it.

Robin Day
Thanks, I'll check this out.
Jeff