Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 是否使用默认函数传递参数或确定插入值?_Sql Server_Function_Sql Server 2008_Default Value - Fatal编程技术网

Sql server 是否使用默认函数传递参数或确定插入值?

Sql server 是否使用默认函数传递参数或确定插入值?,sql-server,function,sql-server-2008,default-value,Sql Server,Function,Sql Server 2008,Default Value,在MS-SQL Server中,是否可以将参数传递给默认函数,或者以某种方式将默认值基于插入记录中的值 可以在MS-SQL Server中将函数指定为列的默认值。使用这个,我试图实现以下目标。对于具有研究ID和患者ID的表,我希望在该研究中自动分配一个新的患者编号 CREATE TABLE [dbo].[PATIENT_STUDY]( [PatientStudyID] [int] IDENTITY(1,1) NOT NULL, [PatientID] [int] NOT NULL

在MS-SQL Server中,是否可以将参数传递给默认函数,或者以某种方式将默认值基于插入记录中的值

可以在MS-SQL Server中将函数指定为列的默认值。使用这个,我试图实现以下目标。对于具有研究ID和患者ID的表,我希望在该研究中自动分配一个新的患者编号

CREATE TABLE [dbo].[PATIENT_STUDY](
    [PatientStudyID] [int] IDENTITY(1,1) NOT NULL,
    [PatientID] [int] NOT NULL,
    [StudyID] [int] NOT NULL,
    [PatientCode] [varchar(30)] NULL,
    [ActiveParticipant] [tinyint] NULL -- 1=yes
)

-- set function as DEFAULT for column PatientCode
ALTER TABLE PATIENT_STUDY
ADD CONSTRAINT 
    DF_PATIENT_STUDY_CODE
    DEFAULT([dbo].[fn_Generate_PatientCode]())
    FOR PatientCode

GO
因此,例如,当患者被添加到STID=67时,例如,使用研究代码012,该研究的最后一个患者代码是012-00024,那么下一个患者代码应该是012-00025

ID  PatientID StudyID PatientCode Active
--- --------- ------- ----------- -------
101 92        65      '009-00031' 1
102 93        66      '010-00018' 1
103 94        67      '012-00023' 1
104 95        67      '012-00024' 1
我知道这可以通过INSERT触发器实现,但我想知道默认情况下是否也可以,因为这是一个更干净、更易于维护的解决方案

我试过以下方法

CREATE FUNCTION [dbo].[fn_Generate_PatientCode]()
RETURNS VARCHAR(10) -- for example "012-00345"
AS 
BEGIN
    -- variables
    DECLARE @Code_next INT
    DECLARE @Code_new VARCHAR(10)

    -- find values of current record, how?
    DECLARE @STID INT
    SET @STID = ?? -- How to determine current record? <--------------------- !!

    -- determine the studycode, example '023-00456' -> '023'
    SET @StudyCode = (SELECT StudyCode FROM Study WHERE StudyID = @STID)

    -- determine max study nr per study, example '123-00456' -> 456
    SET @Code_next = (SELECT MAX(SUBSTRING(PatientCode, 5, 5))
                FROM PATIENT_STUDY
                WHERE IsNumeric(SUBSTRING(PatientCode, 5, 5)) = 1
                AND StudyID = @STID
                ) + 1 -- get next number

    -- check if first patient in this study
    IF (@Code_next is null) 
    BEGIN
        SET @Code_next = 1
    END

    -- prefix with zeroes if needed
    SET @Code_new = @Code_next
    WHILE (LEN(@Code_new) < 5) SET @Code_new = '0' + @Code_new

    -- build new patient code, example '012-00025'
    SET @Code_new = @StudyCode + '-' + @Code_new

    -- return value
    RETURN @Code_new
END

不,我认为没有触发器你是做不到的。

如果你可以按顺序分配,我建议你使用标识列或顺序。您可以将012-00025构建为一个计算列。现在我考虑一下,您如何使用INSERT触发器来实现这一点?我的意思是,您必须在INSERT触发器中确定MAXPatientCode,但是如果您批量插入所有患者,他们将拥有相同的patientcode..@BdR查找Instead of触发器。通过添加一个使用插入表上光标的插入后触发器,我得到了一个有效的解决方案。它遍历所有插入的记录,并为每个记录设置PatientCode。