2009-10-15 17 views
28

Non ho mai lavorato con le funzioni del database, ma il mio progetto attuale lo richiede. Devo inserire una query sql comune in una funzione in modo da non doverla digitare nel nostro codice centinaia di volte. Ho creato la funzione, ma non so come usarla.Come si verifica una funzione con valori di tabella in SQL Server Management Studio?

Ecco il codice funzione:

 
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 
    ) 

Così come dovrei utilizzare questo in una query SQL? Utilizzo semplicemente SELECT fn_GetConfigurationByProfile ('DEFAULTPROFILE')?

Questa potrebbe essere una domanda amatoriale, ma vabbè. Ho bisogno di aiuto :)

risposta

37

si desidera utilizzare DA

Esempio:

select ... 

    FROM fn_GetConfigurationByProfile('DEFAULTPROFILE') 

SQL Server User-defined Functions

+0

Grazie per la risposta tempestiva. Mi sento come un noob totale di non avere però questo dato che era una funzione valutata a livello di tabella:/ – Jeff

+0

leggi come mi è stato dato.hope che ti aiuterà di più :) – anishMarokey

3
SELECT * 
FROM dbo.fn_GetConfigurationByProfile('DEFAULTPROFILE') 
1

Lo si utilizza nella clausola FROM, ad esempio:

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

È possibile anche partecipare a tavoli o utilizzare una clausola in cui contro di essa, tra le altre cose.

7

provare questo

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

Altri hanno mostrato come si può chiamare la funzione tabella all'interno di una query standard. Tuttavia, posso suggerire che potresti preferire creare una vista piuttosto che una funzione?

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 

Quindi è possibile utilizzarlo nel proprio SQL in questo modo.

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

Avrete le opzioni aggiuntive di indicizzare la vista e anche di filtrare su altri dati facilmente se necessario.

+0

Grazie, controllerò questo. – Jeff

Problemi correlati