Estoy enfrentando el problema de convertir en fecha y hora.
DECLARE @sql nvarchar(max)
DECLARE @Lastdate datetime
SET @Update = N'
SELECT TOP 1
'+@Lastdate+' = Purchase.LastUpdated
FROM Purchase
WHERE ID = 15
'
LastUpdated
también es el formato de fecha y hora en la base de datos
Entonces, ¿por qué no puede convertir?
Quiero establecer la fecha y hora en la variable declarada @LastDate de purchase.lastupdate usando solo sp_executesql
Este es otro enfoque posible para establecer el valor de @LastUpdated
usar sp_executesql
:
-- Declaration
DECLARE @sql nvarchar(max)
DECLARE @LastUpdated datetime
DECLARE @err int
-- Statement
SET @sql = N'
SELECT TOP 1 @LastUpdated = Purchase.LastUpdated
FROM Purchase
WHERE ID = 15
'
-- Execution
EXEC @err = sp_executesql
@sql,
N'@LastUpdated datetime OUTPUT',
@LastUpdated OUTPUT
-- Test output
IF @err = 0
PRINT @LastUpdated
ELSE
PRINT 'Error'
Este artículo se recopila de Internet, indique la fuente cuando se vuelva a imprimir.
En caso de infracción, por favor [email protected] Eliminar
Déjame decir algunas palabras