SQL Server - From dinamico

 
Vista:
sin imagen de perfil

From dinamico

Publicado por Daniel (1 intervención) el 11/04/2017 19:34:40
HOLA BUENA TARDE ME GUSTARIA SOLICITARLES AYUDA EN LO SIGUIENTE,
LOS LUGARES EN ESTA CONSULTA DONDE DICE "CONDOTOBPROD" DEBE SER REEPLAZADO CON LA VARIABLE @Fct ALGUNA RECOMENDACION Y/O IDEA QUE ME PUEDAN FACILITAR.
DE ANTE MANO MUCHAS GRACIAS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
USE [dbCol_CHCDataNielsenRetail]
GO
/****** Object:  StoredProcedure [dbo].[sp_FACT]    Script Date: 4/11/2017 11:20:38 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER  procedure [dbo].[sp_FACT]
	@Sales nvarchar (max),
	@Fct nvarchar (max),
	@Prod nvarchar (max)
 
	as
	begin
	BEGIN
		set nocount on
 
	declare @tag nvarchar (max)
	declare @display nvarchar (max)
	declare @sql nvarchar (max)
	declare @press nvarchar (max)
	declare @long nvarchar (max)
	declare @level nvarchar (max)
	declare @LEVEL_NAME nvarchar (max)
		set @sql = 'Select MKT_TAG,
					PROD_TAG,
					pro.LONG '
 
		set @LEVEL_NAME = 2
		while @LEVEL_NAME <= (	SELECT MAX (distinct([HIER_LEVEL_NUM]))
						FROM CONDOTOBPROD
						WHere [HIER_LEVEL_NAME] != 'ITEM'
						and [HIER_LEVEL_NAME] != 'CATEGORY'
						and [HIER_LEVEL_NAME] != 'TOTAL')
	BEGIN
 
		set @level =  (select TOP 1[HIER_LEVEL_NAME] from CONDOTOBPROD where [HIER_LEVEL_NUM] = @LEVEL_NAME)
		set @LEVEL_NAME = @LEVEL_NAME + 1
		set @sql += ',['+@level+']'
		--print @level
	end
 
		set @display = 1
 
	while @display <= (SELECT COUNT(TAG) FROM SISCIRTBFCT )
	BEGIN
		set @tag =   (select [TAG] from SISCIRTBFCT where [DISPLAY_ORDER] = @display)
		set @tag = '[' +@tag+']'
		set @press =  (select [PRECISION] from SISCIRTBFCT where [DISPLAY_ORDER] = @display)
		SET @press = ''+@press+''
		set @long =(Select case when [LONG] like '%(000)%' then REPLACE([LONG],'(000)','')
		when [LONG] like '%(''000)%' then REPLACE([LONG],'(''000)','')
		when [LONG] like '%(''000000)%' then REPLACE([LONG],'(''00000)','')
		when ([LONG] not like '%(000)%') or ([LONG] not like '%(''000)%') or ([LONG] not like '%(''000000)%') then [LONG]
		end as [LONG]
		from SISCIRTBFCT where [DISPLAY_ORDER] = @display)
 
		set @long = '['+@long+']'
		set @display = @display + 1
		set @sql += ',cast(ventas.'+''+@tag+''+' as float)  / power(10,'+''+@press+''+') as '+''+@long+''
		--print @tag
		--print @long
	END
	end
 
		set @sql += 'from  ['+@Sales+']  as ventas
		left join ['+@Prod+'] as pro on ventas.PROD_TAG = pro.TAG'
		--PRINT @SQL
		EXECUTE sp_executesql @sql
		--eXECUTE sp_executesql @tag
		--eXECUTE sp_executesql @press
		--eXECUTE sp_executesql @long
	end
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

From dinamico

Publicado por JAMS (93 intervenciones) el 12/04/2017 16:49:12
Buenos días lo que quieres hacer se puede resolver de la siguiente manera y el tema es SQL Dinámico puedes buscar en san google para encontrar información al respecto

aquí un ejemplo simple que muestra como implementarlo


declare @tbl varchar(100),@sql varchar(100)
set @tbl='esquema.tabla'

set @sql='select * from ' +@tbl
exec(@sql)

Espero que te sirva

Saludos y Suerte
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar