Update statement with variables and answer coming from an API
up vote
0
down vote
favorite
I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.
Then I query google.maps with the addresses and collect the driving distance in km.
If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.
The following code works fine:
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=MYGOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
Now I try to make it work to update all entries that have been modified 5 minutes ago:
Begin transaction
update absences set absences.FreeTextField_15=(
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=GOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
)
where absences.type = '138' and absences absences.sysmodified <(select dateadd(mi,-5,getdate()))
issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.
Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).
My SQL knowledge is home made by trial and errors and step by step learning.
Thanks for your nice cooperation.
add a comment |
up vote
0
down vote
favorite
I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.
Then I query google.maps with the addresses and collect the driving distance in km.
If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.
The following code works fine:
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=MYGOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
Now I try to make it work to update all entries that have been modified 5 minutes ago:
Begin transaction
update absences set absences.FreeTextField_15=(
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=GOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
)
where absences.type = '138' and absences absences.sysmodified <(select dateadd(mi,-5,getdate()))
issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.
Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).
My SQL knowledge is home made by trial and errors and step by step learning.
Thanks for your nice cooperation.
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that runupdate absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
– Nick.McDermaid
Nov 12 at 0:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.
Then I query google.maps with the addresses and collect the driving distance in km.
If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.
The following code works fine:
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=MYGOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
Now I try to make it work to update all entries that have been modified 5 minutes ago:
Begin transaction
update absences set absences.FreeTextField_15=(
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=GOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
)
where absences.type = '138' and absences absences.sysmodified <(select dateadd(mi,-5,getdate()))
issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.
Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).
My SQL knowledge is home made by trial and errors and step by step learning.
Thanks for your nice cooperation.
I'm trying to finalize what can seem a very basic query but I got lost.
Basically, I'm looking for entries in a SQL table, created some mins ago and from that I collect addresses and put in variables.
Then I query google.maps with the addresses and collect the driving distance in km.
If I do the tasks step by step, I can find records, I get google answer (the right one!, yeah) and the issue is now to run everything in one job and schedule it.
The following code works fine:
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = '2158') --must be ab.hid repalced by fixed value for testing
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=MYGOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
Now I try to make it work to update all entries that have been modified 5 minutes ago:
Begin transaction
update absences set absences.FreeTextField_15=(
DECLARE
@ToAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock)
where cmp_type = 'd' and administration = (select comp from humres with (nolock) where res_id = ab.empid)),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@FromAddress NVARCHAR(100) =
(select replace((select
(select oms60_0 from land with (nolock) where landcode = cicmpy.cmp_fctry) COLLATE Latin1_General_CI_AI +'+'+cmp_fpc+'+'+cmp_fcity+'+'+cmp_fadd1
from cicmpy with (nolock) where cmp_wwn =
(select account from projectaccounts with (nolock) where project =
(select top 1 projectnumber from prproject where projectnumber = ab.projectnumber))),' ','+')
from absences ab where ab.type = '138' and hid = ab.hid)
,@DistanceInKm FLOAT
,@Object INT
,@ResponseonseText NVARCHAR(4000)
,@StatuserviceUrl NVARCHAR(500)
SET @StatuserviceUrl = 'https://maps.googleapis.com/maps/api/distancematrix/xml?origins=' + @ToAddress + '&destinations=' + @FromAddress +'&mode=driving&language=en-EN&units=metric,NY&key=GOOGLEMAPSKEY'
EXEC sp_OACreate 'MSXML2.XMLHTTP', @Object OUT; EXEC sp_OAMethod @Object, 'open', NULL, 'get', @StatuserviceUrl, 'false' EXEC sp_OAMethod @Object, 'send' EXEC sp_OAMethod @Object, 'responseText', @ResponseonseText OUTPUT
DECLARE @Response XML
SET @ResponseonseText = REPLACE(@ResponseonseText, N'encoding="UTF-8"', N'')
SET @Response = CAST(CAST(@ResponseonseText AS NVARCHAR(MAX)) AS XML)
DECLARE @Status NVARCHAR(20) DECLARE @Distance NVARCHAR(20)
SET @Status = @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'NVARCHAR(20)')
IF(@Status = 'ZERO_RESULTS') SET @Distance = NULL ELSE SET @Distance = @Response.value('(DistanceMatrixResponse/row/element/distance/value)[1]', 'NVARCHAR(20)')
SET @DistanceInKm = ROUND(CAST(@Distance AS FLOAT) / 1000, 1)
PRINT @DistanceInKm
)
where absences.type = '138' and absences absences.sysmodified <(select dateadd(mi,-5,getdate()))
issue is= incorrect syntax near @DECLARE (the initial one and the ) after PRINT @DistanceInKm is underlined in red.
Another question hitting my head: as my update statement is the main I feel I miss a where clause to define the record id (called ab.hid in the subqueries to find the addresses).
My SQL knowledge is home made by trial and errors and step by step learning.
Thanks for your nice cooperation.
edited Nov 11 at 22:31
fewlinesofcode
1,876515
1,876515
asked Nov 11 at 13:40
Frédéric Morreale
11
11
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that runupdate absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
– Nick.McDermaid
Nov 12 at 0:49
add a comment |
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that runupdate absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated
– Nick.McDermaid
Nov 12 at 0:49
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run
update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated– Nick.McDermaid
Nov 12 at 0:49
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run
update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated– Nick.McDermaid
Nov 12 at 0:49
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249323%2fupdate-statement-with-variables-and-answer-coming-from-an-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I suggest you first wrap this in a stored procedure. That will let you unit test the stored procedure. Then you can call the proc from the SQL Agent job and that will all be a bit neater. The syntax in your second part is definitely incorrect. You need to run all that code and after that run
update absences set FreeTextField_15 = @ToAddress. But yes you're going to need to use a cursor to step through records one by one and update them one by one. Your existing code will not work properly if more than one record was updated– Nick.McDermaid
Nov 12 at 0:49