0

我对 SQL Server 2008 编程和尝试创建过程非常陌生。

好吧,要求是“该过程根据输入参数返回数据,或者如果没有给出输入数据,它应该执行默认选择并返回所有符合条件的数据”

我试过这样的东西-

CREATE PROCEDURE [dbo].[Proc_sampletestproc]
   (@testid int)
AS
BEGIN         
   SET NOCOUNT OFF;  ----I worked with Oracle PL/SQL and so do we need for mandatory this declarations

在这里,我需要检查输入testid是否有值。如果它存在,我们有一个带有选择的案例,或者我们进行默认选择。

另外,我SELECT在表上放置了一个没有连接的直接。因为可能有保险,也可能没有保险,我怎么JOIN办?我的意思是语法 - SQL Server 中的语法完全不同。OUTER JOINStestid

SELECT 
    T.TESTID, T.NAME, TI.INSURENAME 
FROM 
    testinsured ti, test t, testinsuredHistory tih
WHERE
    t.testid = ti.testid  -----This entry may be there or not IN THE testinsured TABLE
    AND tih.testinsuredid = ti.testinsuredid --A testid might have 2 Insurers whose history is stored here.
    AND TIH.STARTDATE IS NOT NULL
    AND TIH.ENDDATE IS NOT NULL     --TO CHECK ACTIVE DATES FOR COVERAGE

另外,我想进行分组,testid以便名称出现一次,但相应地InsuredPlanname出现一次,是每个名称的两倍Testid

4

1 回答 1

0
if (@testid is not null)
begin
    /* ... */
end
else 
begin
    SELECT T.TESTID,T.NAME,TI.INSURENAME 
    FROM test t 
    left outer join testinsured ti on t.testid = ti.testid -----This entry may be there or not IN THE testinsured TABLE
    left outer join testinsuredHistory tih on ti.testinsuredid = tih.testinsuredid --A testid might have 2 Insurers whose history is stored here.
    where TIH.STARTDATE IS NOT null AND TIH.ENDDATE IS NOT NULL
    /* group by ... */
end
于 2013-04-09T12:31:01.680 回答