sql mssql,外部应用,多选左连接和外部aaply所以它不会返回多个(lef join)结果

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql mssql,外部应用,多选左连接和外部aaply所以它不会返回多个(lef join)结果相关的知识,希望对你有一定的参考价值。

USE [portalfornecedores]
GO
/****** Object:  StoredProcedure [dbo].[portal_concurso_getArtigos]    Script Date: 07/10/2015 16:14:01 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ricardo Antunes
-- Create date: 07/10/2015
-- Description:	data for concurso reply by fornecedor
-- =============================================
ALTER PROCEDURE [dbo].[portal_concurso_getForReply] 
	-- Add the parameters for the stored procedure here
	 @id_concurso int = 0, 
	 @reg_start int = 0,
	 @reg_end int = 0
AS
BEGIN 	
	--header select (concurso)		
	SELECT * FROM (SELECT * FROM portal_concurso AS c
	WHERE c.id_concurso = @id_concurso) AS header;

	-- select artigos
	SELECT  * FROM
	( 
		SELECT ROW_NUMBER() OVER ( ORDER BY a.id_artigo ) AS RowNum,
		a.[id_artigo], a.[id_artigo_sicaf], a.[quantidade], sa.id_cartigo, sa.descricao,
		sb.[ref_forn], sc.[data_ini], sc.[data_fim], sc.[quantidade] as qtdComercial,
		sc.[moeda], sc.[preco_base_moeda], sc.[cambio], sc.[preco_base_euros], sc.[unidade_base],
		sc.[desconto1], sc.[desconto2], sc.[desconto3], sc.[lead_time]
		FROM portal_concurso_artigo as a
		INNER JOIN sicaf_artigo AS sa ON a.id_artigo_sicaf = sa.id_cartigo
		LEFT JOIN sicaf_fornecedor_artigo AS sb ON a.id_artigo_sicaf = sb.id_cartigo
		--LEFT JOIN sicaf_condcomerciais_artigo AS sc ON sb.id = sc.id_artforn
		OUTER APPLY (select top 1 * from sicaf_condcomerciais_artigo sc
		 where sc.id_artforn = sb.id order by data_fim ) sc
		WHERE a.id_concurso = @id_concurso
	) AS result
	WHERE RowNum >= @reg_start AND RowNum <= @reg_end
	ORDER BY RowNum;
	
	-- select count
	SELECT  COUNT(*) as num_rows
	FROM portal_concurso_artigo as a
	WHERE a.id_concurso = @id_concurso ;
END

以上是关于sql mssql,外部应用,多选左连接和外部aaply所以它不会返回多个(lef join)结果的主要内容,如果未能解决你的问题,请参考以下文章

组合不同(冗长)SQL 语句的列的最简单方法 - MSSQL

当 SQL Server(mssql-jdbc) 遇上 BigDecimal → 精度丢失,真坑!

SQL Server 外部应用查询优化

如何将外部数据库 导入到系统的SQL中

IIS 之外的 Delphi 7 ADO 连接池

在多列上组合 sql 内部和外部联接