0

introducir la descripción de la imagen aquí

Estoy creando un query que toma un vista previamente creada donde aplico un pivot dinamico para convertir mis filas en columnas (creo un pivot dinamico ya que despues se agregaran mas filas) el problema es que al ejecutarlo me envia un error que muestro mas abajo en la publicacion

En la imagen muestro la tabla o la consulta que solo muestra esos 3 columnas feature es la que estoy tratando de convertir de filas a a a columnas

Al revisar o buscar una solicion encontré que se podía solucionar asinando alas variable el valor max

Como visualizan en el query ya cambie el valor de las variables a max y aun asi me sigue mostrando este error.

`declare @query varchar(MAX)
declare @Columns varchar(MAX)
--Optenemos la columnas pivot
select
@Columns=STUFF((select Distinct'],['+ Feature FROM FNA_vw_WitersTestCase
order by '],['+ Feature for XML PATH('') ),1,1,'')+']'
set @query ='select *
from (
select Feature,TC,DATyENAME(week,Date) as Weeks,DATENAME(year,Date) as Years FROM FNA_vw_WitersTestCase
)Cs
pivot (
max(TC) FOR[Feature] IN ('+@Columns+')
)Pv
'
execute (@query)`


--El error que me muestra es el siguiente;

    Msg 102, Level 15, State 1, Line 27
Incorrect syntax near ','.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'ALERT-FRD-REQ-212687/C-Alerts - Chimes/Prompts/Beeps/Audio Attenuation [CTR],[Alerts-Chimes-Prompts-Attenuation [SyncG3],[AM- FM' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Bezel Diagnostics [CTR],[Bezel Diagnostics [SyncG3],[Bezel Diagnostics [SyncG4],[Boundary Alert [SyncG3],[BT Phone [SyncG3],[BTP' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Climate Control [SYNC4],[CLK-FRD-REQ-017552/A-APIM Clock Strategy [CTR],[Connectivity Customer Opt-In Client [Sync G3],[Consider' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Digital A2B Audio Bus [SyncG3],[Digital Owners Manual [SyncG4],[Digital RVC [SyncG3],[Digital RVC [SyncG4],[Display Mode (Day N' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Frunk Softswitch [SyncG4],[GAL-FRD-REQ-213592/C-GAL [CTR],[Global Clock [SyncG3],[Google Automotive Link [SyncG3],[HUD Feature C' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'In-Vehicle Software Update (IVSU) [SyncG3],[LBP-FRD-REQ-213638/B-List Browser Protocol [CTR],[Lincoln Backup Ignition [SyncG4],[' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Rear Camera On Demand [SyncG3],[Rear Seat Entertainment [SyncG3],[Rear Seat Entertainment [SyncG4],[Rear View Camera [SyncG3],[R' is too long. Maximum length is 128.

Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'SIMA V2 - Steering Wheel Horizon Controller [Phoenix],[Sirius Satellite Radio v2 [SyncG3],[Station Management [SyncG 4.1],[Stati' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Trailer Backup Assist v2 [SyncG3],[Trailer Backup Assist v2 [SyncG4],[Trailer Perimeter Alarm [SyncG4],[Trailer Reverse Guidance' is too long. Maximum length is 128.
Msg 103, Level 15, State 4, Line 27
The identifier that starts with 'Vehicle Settings [SyncG 4.1],[Vehicle Settings [SyncG3],[Video On Demand [SyncG3],[Visual Park Assist [SyncG3],[VOL-FRD-REQ-2127' is too long. Maximum length is 128.
Msg 105, Level 15, State 1, Line 27
Unclosed quotation mark after the character string 'Vehicle Settings [SyncG 4.1],[Vehicle Settings [SyncG3],[Video On Demand [SyncG3],[Visual Park Assist [SyncG3],[VOL-FRD-REQ-21270'.

1 respuesta 1

1

Uno de los problemas que tienes es que estás usando valores muy largos para ser los nombres de tus columnas. Los nombres de columnas deben de tener una longitud máxima de 128 caracteres. Esto se puede solucionar parcialmente cambiando PIVOT por un agregado condicional. Esto te da una mayor versatilidad al momento de convertir filas a columnas y tiene un rendimiento igual o mejor a la instrucción PIVOT.

DECLARE @query varchar(MAX);


SET @query = 'SELECT DATENAME(week,Date) as Weeks ' + CHAR(10)
           + '       ,DATENAME(year,Date) as Years ' + CHAR(10)
           + (SELECT DISTINCT '       ,MAX( CASE WHEN Feature = '''+ REPLACE( Feature, '''', '''''') + ''' THEN TC END) AS ' + QUOTENAME( LEFT( Feature, 126)) + CHAR(10)
              FROM FNA_vw_WitersTestCase
              ORDER BY 1
              FOR XML PATH(''), TYPE).value('./text()[1]', 'varchar(max)')
           + 'FROM FNA_vw_WitersTestCase ' + CHAR(10)
           + 'GROUP BY DATENAME(week,Date) ' + CHAR(10)
           + '       ,DATENAME(year,Date);';

PRINT @query;
EXECUTE(@query);
  
1
  • ok, muchas gracias por la ayuda y los tips, me sirvio de mucho , muchas gracias
    – jxDev
    Commented el 30 jun. 2022 a las 17:23

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.