以下是我的存储过程的一部份。
if exists(SELECT    *
FROM         global_user_position_tb 
 where global_user_position_tb.streetid=-1 and global_user_position_tb.username=@username ) 
begin
/*我想保存下面这个查询为存储过程的其余部分使用怎么办*/
SELECT   basicinfo_tb.globalid   FROM       basicinfo_tb   
end/*我想在这里使用上面这个查询怎么办*/

解决方案 »

  1.   

    用个临时表就可以了
    if exists(SELECT    *
    FROM  global_user_position_tb 
    where global_user_position_tb.streetid=-1 and   
         global_user_position_tb.username=@username) 
    begin
    select * into #temp from (SELECT  basicinfo_tb.globalid   FROM basicinfo_tb )
    end
    -------
     --select * from #temp
      

  2.   

    可以放到临时表,也可以直接作为结果集。1.SELECT  basicinfo_tb.globalid into # FROM basicinfo_tb2.select * from (SELECT  basicinfo_tb.globalid FROM basicinfo_tb)a
      

  3.   

    ALTER PROCEDURE dbo.StoredProcedure1 
        @username NVarChar(30)  -- 用户名称
      
    AS
    /******************************************************************************
    **
    ** 得到该用户所管辖的所有人员,当用户是直属于社区的情况下
    **
    *******************************************************************************/
     IF   EXISTS
                                 (SELECT     username, districtId, streetId, communityId
                                  FROM         global_user_position_tb
                                  WHERE     (communityId <> - 1) AND (streetId <> - 1) AND (username = @username))
                                       BEGIN
                                    SELECT     GlobalId
                                    INTO            [#tmp]
                                    FROM         (SELECT     basicinfo_tb.GlobalId
                                                           FROM          basicinfo_tb INNER JOIN
                                                                                  global_user_position_tb ON basicinfo_tb.street = global_user_position_tb.streetId AND global_user_position_tb.username = @username) END
    为什么老是提示"关键字end附近有错误"??????????????????
      

  4.   

    ALTER PROCEDURE dbo.StoredProcedure1 
        @username NVarChar(30)  -- 用户名称
      
    AS
    /******************************************************************************
    **
    ** 得到该用户所管辖的所有人员,当用户是直属于社区的情况下
    **
    *******************************************************************************/
     IF   EXISTS
                                 (SELECT     username, districtId, streetId, communityId
                                  FROM         global_user_position_tb
                                  WHERE     (communityId <> - 1) AND (streetId <> - 1) AND (username = @username))
                                       BEGIN
                                    SELECT     GlobalId
                                    INTO            [#tmp]
                                    FROM         (SELECT     basicinfo_tb.GlobalId
                                                           FROM          basicinfo_tb INNER JOIN
                                                                                  global_user_position_tb ON basicinfo_tb.street = global_user_position_tb.streetId AND global_user_position_tb.username = @username) A  END--加一个别名
      

  5.   

    临时表生成在Temp数据库中,是看不到的,你定义的是局部临时表,只在当前连接有效!
      

  6.   

    两种方法:
    --第一种方法:直接用存储过程返回的数据集select * from openrowset('sqloledb','Trusted_Connection=yes','exec 数据库名.dbo.存储过程名') select * from openrowset('sqloledb','Trusted_Connection=yes','exec 数据库名..存储过程名') select * from openrowset('sqloledb','localhost';'用户名';'密码','exec 数据库名..存储过程名')select * from openrowset('sqloledb','192.168.0.1';'用户名';'密码','exec 数据库名..存储过程名')-第二种方法:先创建临时表,然后再追加create table #t(...)insert into #t exec 存储过程名select * from #t
      

  7.   

    直接select count(*) from #tmp 就可以阿
      

  8.   

    请问
    functionId, global_remainder_tb.globalid, global_remainder_tb.num, global_remainder_tb.pagename,global_remainder_tb.pagepath, global_remainder_tb.content
    114 1342 1 111 NULL NULL
    114 1342 1 111 NULL NULL
    现在我只要得到一个,我看到distinct的使用方法,好像都是根据一个列来distinct ,而我要根据前三个列来distinct,就是说前三个可以说是我的主键,请问如何做。
      

  9.   

    --直接distinct就可以阿。distinct会消除完全重复的行。declare @T table(functionId int,globalid int,num int,pagename int,pagepath int,content int)
    insert into @t select 114,1342,1,111,NULL,NULL
    union all select 114,1342,1,111,NULL,NULLselect distinct * from @t