USE ProductCompendium
GO

DECLARE @phar INT = 2759829
DECLARE @coNr INT = 13368
DECLARE @priTyp NVARCHAR(6) = N'PGRO'

SELECT * FROM dbo.ART_Art WHERE Pharmacode = @phar
SELECT * FROM dbo.PA_Pa WHERE CompanyNr = @coNr
SELECT * FROM dbo.ART_CoPri WHERE Code = @priTyp

DECLARE @artPK INT = ( SELECT Art_PK FROM ART_Art WHERE Pharmacode = @phar )
DECLARE @coPK INT = ( SELECT Pa_PK FROM dbo.PA_Pa WHERE CompanyNr = @coNr )
DECLARE @ptPK TINYINT = ( SELECT CoPri_PK FROM dbo.ART_CoPri WHERE Code = @priTyp )

SELECT ArtPri_PK, Art_FK, Pa_FK, CoPri_FK, FromDate, ToDate, Price, WithdrawnDate, Db_Status, Db_InsDt, Db_UpdDt, DATEADD(MINUTE, -1, FromDate) AS CouldBeToDate
FROM dbo.ART_ArtPri
WHERE Art_FK = @artPK AND Pa_FK = @coPK AND CoPri_FK = @ptPK

DECLARE @apPK INT = 0
DECLARE @td VARCHAR(100) = ''

SELECT ' UPDATE ART_ArtPri SET ToDate = ''' + @td + ''' WHERE ArtPri_PK = ' + CAST(@apPK AS VARCHAR(10))