Código SQL:
Ver originalDECLARE @PrimaryKeyName AS VARCHAR(100)
DECLARE @tabla AS VARCHAR(100)
DECLARE @ID AS INT
SET @tabla='t_q_users'
--Obtengo el nombre del primary key de la tabla
SELECT @PrimaryKeyName =
COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE a
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS b
ON a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
WHERE a.TABLE_NAME = @Tabla AND constraint_type = 'Primary key'
--Obtengo el ID actual de la tabla
print @tabla
DECLARE @query VARCHAR(MAX)
print @PrimaryKeyName
SET @query= ' SELECT MAX( ' + @PrimaryKeyName + ') as id into ##temp FROM ' + @Tabla
EXEC Sp_sqlExec @query
SELECT @id=id FROM ##temp
DROP TABLE ##temp
print @id
Prueba con algo asi :)
Saludos!