将多个数据库表合并为一个视图

时间:2017-02-07 08:15:01

标签: asp.net sql-server vb.net

如何在一个视图中显示两个不同数据库表中的数据?我想做的是放置" CreateBy"数据从Table2进入" CreateBy" Table1中的数据。

这两个都来自同一个数据库,但它们位于不同的表中。

这是我要修改的Table1图片(列:创建者):

Picture of Table1

这是Table2的图片,我想从(Column:CreateBy)中获取数据:

Picture of Table2

以下是我目前正在使用的代码:



ALTER Procedure [dbo].[GetJobsReportListing]
	@Page int,
	@PageSize int,
	@TotalRecords int Output,
	@Webmaster varchar(50) = null,
	@consultantGUID varchar(50)= null,
	@CreateByGUID varchar(50)= null,
	@branchGUID varchar(50)= null,
	@OrderStatusGUID varchar(50) = null,
	@strYear varchar(10) = null,
	@strMonth varchar(10) =null
	
AS 
	
BEGIN
	BEGIN TRY
	begin transaction
	
	Declare @IsAdmin bit

	set @IsAdmin = (Select Top 1 IsAdmin From TBL_ConsultantUser Where ConsultantUserGUID=@Webmaster)

		if (@IsAdmin = '1')
			Begin
				set @Webmaster = null
			End
		Else
			Begin
			Declare @ConsultantRole varchar(50)

			set @ConsultantRole = (Select Top 1 ConsultantRoleGUID From TBL_ConsultantUser Where ConsultantUserGUID=@Webmaster)

			if (@ConsultantRole = '9199227d-1d40-4e7e-b532-cb4f3b3d9982')
				Begin
					set @branchGUID = (Select Top 1 BranchGUID From TBL_ConsultantUser Where ConsultantUserGUID=@Webmaster)
					set @Webmaster = null
				End
		End
		
		Declare @TBLReport as Table (
									[JobNo][varchar](50),
									[CompanyName][varchar](350),
									[JobPositionName][varchar](250),
									[SalaryRange][varchar](100),
									[JobBillRate][float],
									[JobCoordinatorGUID][varchar](50),
									[JobCoordinator][varchar](300),
									[BranchName][varchar](250),
									[CreateByGUID][varchar](50),
									[CreateByName][varchar](300),
									[OrderStatusName][varchar](100),
									[CreateDate][Datetime]
								)
		Insert Into @TBLReport
		(
			[JobNo],
			[CompanyName],
			[JobPositionName],
			[SalaryRange],
			[JobBillRate],
			[JobCoordinatorGUID],
			[JobCoordinator],
			[BranchName],
			[CreateByGUID],
			[CreateByName],
			[OrderStatusName],
			[CreateDate]
		)	
		Select 
			TBL_JobPost.JobNo,
			TBL_Company.CompanyName,
			TBL_LK_JobPosition.JobPositionName,
			TBL_LK_Salary.SalaryRange,
			TBL_JobPost.JobBillRate,
			cuser_2.ConsultantUserGUID as 'JobCoordinatorGUID',
			cuser_2.ContactPerson as 'JobCoordinator',
			DDLBranch.BranchName,
			createby_1.CreateBy as 'CreateByGUID',
			cuser_1.ContactPerson,
			TBL_LK_OrderStatus.OrderStatusName,
			TBL_JobPost.CreateDate
		From
			TBL_JobPost
			Left Join TBL_Company
			On TBL_JobPost.CompanyGUID = TBL_Company.CompanyGUID
			Left Join TBL_LK_JobPosition
			On TBL_JobPost.JobPositionGUID = TBL_LK_JobPosition.JobPositionGUID
			Left Join TBL_LK_Salary
			On TBL_JobPost.SalaryGUID = TBL_LK_Salary.SalaryGUID
			Left Join TBL_LK_OrderStatus
			On TBL_JobPost.JobOrderStatusGUID = TBL_LK_OrderStatus.OrderStatusGUID
			Left Join TBL_ConsultantUser as cuser_1
			On TBL_JobPost.CreateBy = cuser_1.ConsultantUserGUID
			Left Join TBL_JobPostReply as createby_1
			On TBL_JobPost.CreateBy = createby_1.CreateBy
			Left Join TBL_ConsultantUser as cuser_2
			On TBL_JobPost.JobCoordinator = cuser_2.ConsultantUserGUID
			Left Join DDLBranch
			On cuser_2.BranchGUID = DDLBranch.BranchGUID
			Left Join TBL_JobPostReply
			On TBL_JobPost.JobPostGUID = TBL_JobPostReply.JobPostGUID
			Left Join TBL_LK_JobApplicationStatus
			On TBL_JobPostReply.StatusApplicationGUID = TBL_LK_JobApplicationStatus.JobApplicationStatusGUID
		Where
			(
			ISNULL(createby_1.CreateBy,'') = ISNULL(@Webmaster,ISNULL(createby_1.CreateBy,''))	
			Or
			ISNULL(cuser_2.ConsultantUserGUID,'') = ISNULL(@Webmaster,ISNULL(cuser_2.ConsultantUserGUID,''))	
			)
			And
			(
			ISNULL(cuser_1.BranchGUID,'') = ISNULL(@branchGUID,ISNULL(cuser_1.BranchGUID,''))
			Or
			ISNULL(cuser_2.BranchGUID,'') = ISNULL(@branchGUID,ISNULL(cuser_2.BranchGUID,''))
			)
			And
			ISNULL(TBL_JobPost.JobOrderStatusGUID,'') = ISNULL(@OrderStatusGUID,ISNULL(TBL_JobPost.JobOrderStatusGUID,''))
			And
			ISNULL(YEAR(Convert(varchar,TBL_JobPostReply.CreateDate,111)), '') = ISNULL(@strYear,ISNULL(YEAR(Convert(varchar,TBL_JobPostReply.CreateDate,111)), ''))
			AND 
			ISNULL(Month(Convert(varchar,TBL_JobPostReply.CreateDate,111)), '') = ISNULL(@strMonth,ISNULL(Month(Convert(varchar,TBL_JobPostReply.CreateDate,111)), ''))
		Group By
			TBL_JobPost.JobNo,
			TBL_Company.CompanyName,
			TBL_LK_JobPosition.JobPositionName,
			TBL_LK_Salary.SalaryRange,
			TBL_JobPost.JobBillRate,
			cuser_2.ConsultantUserGUID,
			cuser_2.ContactPerson,
			DDLBranch.BranchName,
			createby_1.CreateBy,
			cuser_1.ContactPerson,
			TBL_LK_OrderStatus.OrderStatusName,
			TBL_JobPost.CreateDate
	
	--Get Total Records-- (Editable Code)
	Set @TotalRecords = (SELECT 
							Count(*)as TotalRecords
						 From 
							@TBLReport as vw1
						 Where
							ISNULL(vw1.JobCoordinatorGUID,'') = ISNULL(@consultantGUID,ISNULL(vw1.JobCoordinatorGUID,''))
							AND
							ISNULL(vw1.CreateByGUID,'') = ISNULL(@CreateByGUID,ISNULL(vw1.CreateByGUID,''))
						);
	
	
	--Paging Calculations-- (Fixed Code)
				Declare @EndAtRecord int;
				Set @EndAtRecord = (@Page) * (@PageSize);
				
				If(@PageSize <> '0')
						Begin
							If(@TotalRecords <> '0')
								Begin
									If((@PageSize*@Page) > @TotalRecords)
									Begin
										Set @PageSize = @TotalRecords % @PageSize
									End
								End 
						End
				Else
					Begin
						Set @PageSize = @TotalRecords
						Set @EndAtRecord = @TotalRecords;
					End
	
	
	--Select Statement-- 
	SELECT * FROM (
		SELECT TOP (@PageSize) * FROM (
			--Main SQL-- (Editable Code))
			SELECT TOP (@EndAtRecord) 
				ROW_NUMBER() OVER (ORDER BY vw1.JobNo ASC) AS RowNumber,
				[JobNo],
				[CompanyName],
				[JobPositionName],
				[SalaryRange],
				[JobBillRate],
				[JobCoordinatorGUID],
				[JobCoordinator],
				[BranchName],
				[CreateByGUID],
				[CreateByName],
				[OrderStatusName],
				[CreateDate]
			From 
				@TBLReport as vw1
			Where
				ISNULL(vw1.JobCoordinatorGUID,'') = ISNULL(@consultantGUID,ISNULL(vw1.JobCoordinatorGUID,''))
				AND
				ISNULL(vw1.CreateByGUID,'') = ISNULL(@CreateByGUID,ISNULL(vw1.CreateByGUID,''))
			)as newtbl1
		ORDER by RowNumber DESC) as newtbl2
	ORDER by RowNumber ASC
	
	
	commit transaction	   
	return 1
END TRY

BEGIN CATCH
	rollback transaction
	EXEC InsertDatabaseErrorLog
	return 0
END CATCH

END
&#13;
&#13;
&#13;

0 个答案:

没有答案