Error in executing dynamic SQL Query with table variable in SQL Server 2008 - sql-server-2008

These are the following parameters & table variable used for executing dynamic sql query:
Declaration
DECLARE #sQuery VARCHAR(MAX)
DECLARE #FYear NVARCHAR(10)='2016-2017'
DECLARE #TYear NVARCHAR(10)='2016-2018'
DECLARE #CLAIMSUM TABLE
(
ClaimType NVARCHAR(MAX),
JAN DECIMAL(19,6),
FEB DECIMAL(19,6),
MAR DECIMAL(19,6),
APR DECIMAL(19,6),
MAY DECIMAL(19,6),
JUN DECIMAL(19,6),
JUL DECIMAL(19,6),
AUG DECIMAL(19,6),
SEP DECIMAL(19,6),
OCT DECIMAL(19,6),
NOV DECIMAL(19,6),
DEC DECIMAL(19,6),
TOTAL DECIMAL(19,6)
)
Dynamic SQL Query
SET #sQuery=N'SELECT C1.ClaimType,
SUM(C1.JAN) AS ''JAN '+#FYear+'-'+#TYear+''',SUM(C1.FEB) AS ''FEB '+#FYear+'-'+#TYear+''',SUM(C1.MAR) AS ''MAR '+#FYear+'-'+#TYear+''',
SUM(C1.APR) AS ''APR '+#FYear+'-'+#TYear+''',SUM(C1.MAY) AS ''MAY '+#FYear+'-'+#TYear+''',SUM(C1.JUN) AS ''JUN '+#FYear+'-'+#TYear+''',
SUM(C1.JUL) AS ''JUL '+#FYear+'-'+#TYear+''',SUM(C1.AUG) AS ''AUG '+#FYear+'-'+#TYear+''',SUM(C1.SEP) AS ''SEP '+#FYear+'-'+#TYear+''',
SUM(C1.OCT) AS ''OCT '+#FYear+'-'+#TYear+''',SUM(C1.NOV) AS ''NOV '+#FYear+'-'+#TYear+''',SUM(C1.DEC) AS ''DEC '+#FYear+'-'+#TYear+''',
SUM(C1.TOTAL) AS ''TOTAL''
FROM #CLAIMSUM C1 GROUP BY C1.ClaimType';
EXECUTE #sQuery
Error
When executing this query I am getting following error:
The name 'SELECT C1.ClaimType,
SUM(C1.JAN) AS 'JAN 2016-2017',SUM(C1.FEB) AS 'FEB 2016-2017',SUM(C1.MAR) AS 'MAR 2016-2017',
SUM(C1.APR) AS 'APR 2016-2017',SUM(C1.MAY) AS 'MAY 2016-2017',SUM(C1.JUN) AS 'JUN 2016-2017',
SUM(C1.JUL) AS 'JUL 2016-2017',SUM(C1.AUG) AS 'AUG 2016-2017',SUM(C1.SEP) AS 'SEP 2016-2017',
SUM(C1.OCT) AS 'OCT 2016-2017',SUM(C1.NOV) AS 'NOV 2016-2017',SUM(C1.DEC) AS 'DEC 2016-2017',
SUM(C1.TOTAL) AS 'TOTAL'
FROM #CLAIMSUM C1 GROUP BY C1.ClaimType' is not a valid identifier.

I had replaced EXECUTE #sQuery with EXECUTE (#sQuery) and now it is working fine.

Related

Unable to match an mysql string response in freeradius

I using freeradius 2.2.8 and mysql for sending our coa/disconnect messages via freeradius when a bandwidth quota is hit. That part of the code is working great. Unfortunately for me the coa/disconnect message is sent out even though an Accounting Stop is sent out by the NAS. In order to prevent that from happening I am putting a condition where in I pull the acct termination cause from mysql radacct table and compare it against some strings.
Under accounting,
update control {
....
Tmp-String-0 := "%{sql:SELECT acctterminatecause AS Terminate FROM radacct WHERE radacct.username='%{User-Name}' AND radacct.acctsessionid='%{Acct-Session-Id}'}}"
***if ("%{control:Tmp-String-0}" != "User-Request")***{
if (("%{control:Tmp-Integer-0}" > "%{control:Tmp-Integer-2}") || ("%{control:Tmp-Integer-1}" > "%{control:Tmp-Integer-3}")){
if ("%{control:coa_dm}" == "coa"){
update coa {
User-Name = "%{User-Name}"
Acct-Session-Id = "%{Acct-Session-Id}"
NAS-IP-Address = "%{NAS-IP-Address}"
#Filter-Id = "UN-AUTHORIZED-PROFILE"
Framed-IP-Address = "%{Framed-IP-Address}"
Session-Timeout = 10
}
}
if ("%{control:coa_dm}" == "dm"){
update disconnect {
User-Name = "%{User-Name}"
NAS-IP-Address = "%{NAS-IP-Address}"
}
}
}
}
As per the freeradius debug logs we can see that the string should be a match and the program should avoid the inside if conditions. Unfortunately that is not the case and the 1st IF condition always returns true.
***Thu May 17 17:43:41 2018 : Info: expand: SELECT acctterminatecause AS Terminate FROM radacct WHERE radacct.username='%{User-Name}' AND radacct.acctsessionid='%{Acct-Session-Id}' -> SELECT acctterminatecause AS Terminate FROM radacct WHERE radacct.username='kiranc' AND radacct.acctsessionid='5AFD71CB-3FE1C000'
Thu May 17 17:43:41 2018 : Debug: rlm_sql (sql): Reserving sql socket id: 16
Thu May 17 17:43:41 2018 : Info: sql_xlat finished
Thu May 17 17:43:41 2018 : Debug: rlm_sql (sql): Released sql socket id: 16
Thu May 17 17:43:41 2018 : Info: expand: %{sql:SELECT acctterminatecause AS Terminate FROM radacct WHERE radacct.username='%{User-Name}' AND radacct.acctsessionid='%{Acct-Session-Id}'}} -> User-Request}
Thu May 17 17:43:41 2018 : Info: ++} # update control = noop
Thu May 17 17:43:41 2018 : Info: ++? if ("%{control:Tmp-String-0}" != "User-Request")
Thu May 17 17:43:41 2018 : Info: expand: %{control:Tmp-String-0} -> User-Request}
Thu May 17 17:43:41 2018 : Info: ? Evaluating ("%{control:Tmp-String-0}" != "User-Request") -> TRUE***
Thu May 17 17:43:41 2018 : Info: ++? if ("%{control:Tmp-String-0}" != "User-Request") -> TRUE
Thu May 17 17:43:41 2018 : Info: ++if ("%{control:Tmp-String-0}" != "User-Request") {
Thu May 17 17:43:41 2018 : Info: +++? if (("%{control:Tmp-Integer-0}" > "%{control:Tmp-Integer-2}") || ("%{control:Tmp-Integer-1}" > "%{control:Tmp-Integer-3}"))
Thu May 17 17:43:41 2018 : Info: expand: %{control:Tmp-Integer-0} -> 3169
Thu May 17 17:43:41 2018 : Info: expand: %{control:Tmp-Integer-2} -> 25000000
Thu May 17 17:43:41 2018 : Info: ?? Evaluating ("%{control:Tmp-Integer-0}" > "%{control:Tmp-Integer-2}") -> FALSE
Thu May 17 17:43:41 2018 : Info: expand: %{control:Tmp-Integer-1} -> 13402
Thu May 17 17:43:41 2018 : Info: expand: %{control:Tmp-Integer-3} -> 50000000
I tried the single quotes, double quotes using the & instead of %, but to no avail. The other conditions are met correctly when hit as they are returned as integers.
Thank you for the help.
I forgot to mention that I had got it working. Instead of comparing using Tmp-String-0 variable I just used the actual radius attribute name and got it working.
if (("%{Acct-Terminate-Cause}" != "User-Request")
Thank you for the help.

How to fetch JSON content values dynamically using JSON slurper in groovy

I am trying to filter out the JSON response values by passing on few parameters dynamically. Below is the code.
import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper
responseContent = testRunner.testCase.getTestStepByName("Request 1").getPropertyValue("Response")
jsonresponse = new JsonSlurper().parseText(responseContent)
log.info jsonresponse.context["parameter"]
context["parameter"] stores the values from an Excel sheet and doing only a log.info for context["parameter"], it shows those values correctly. Below is the output of the code log.info context["parameter"].
Thu Dec 14 07:18:53 CST 2017:INFO:firstName
Thu Dec 14 07:18:53 CST 2017:INFO:lastName
Thu Dec 14 07:18:54 CST 2017:INFO:frNum
Thu Dec 14 07:18:54 CST 2017:INFO:notes
But when I execute the code
log.info jsonresponse.context["parameter"]
Result:
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:41 CST 2017:INFO:[]
Thu Dec 14 07:20:42 CST 2017:INFO:[]
Thu Dec 14 07:20:42 CST 2017:INFO:[]
log.info jsonresponse yields the below response out of which I need to get certain values (values stored in the context["parameter"])
Thu Dec 14 07:21:45 CST 2017:INFO:[{errorMessage=null, middleName=null, lastName=Kosnick, frName=Kosnick Steven H , mplastName=null, competeRgnTxt=null, doNum=null, gddUnitStDate=null, fdNum=null, mpfirstName=null, legalEntityID=null, noNum=null, contractType=Financial Rep, frNum=046426, offcNum=NO 091, notes=Active, fullTmeSrvDt=null, firstName=Steven, networkOfficeNum=091}]
Instead of this :
log.info jsonresponse.context["parameter"]
Try this :
log.info jsonresponse[context["parameter"]]
This should resolve your issue :)

To get values from 2D array

I have used this query to retrieve the dates for one particular user's approved leaves -
LeaveRequest.where(user_id: 6).where(status: 1).pluck(:from_date, :to_date)
and I'm getting this array as result -
[[Mon, 12 Sep 2016, Fri, 16 Sep 2016], [Tue, 06 Sep 2016, Tue, 06 Sep 2016], [Thu, 01 Sep 2016, Fri, 02 Sep 2016], [Tue, 30 Aug 2016, Wed, 31 Aug 2016]]
what I want is to fetch all the dates as well as the dates between 12 Sep 2016 and 16 Sep, 2016 (13th 14th and 15th).
I am assuming you mean something like this
require 'date'
#This is to simulate your current Array
current_array = 5.times.map {|n [Date.new(2016,n+1,1).<<(1),Date.new(2016,n+1,1)]}
#map the 2 dates to a Range
new_array = current_array.map{|start_date,end_date| (start_date..end_date)}
new_array.first.class
#=> Range
Calling to_a on the Range will blow it out into all the dates between start_date and end_date
With a rails you could do something like
class LeaveRequest
def self.user_requested_ranges(user_id, status_id)
scoped.
where(user_id: user_id, status: status_id).
pluck(:from_date, :to_date).
map do |from_date, to_date|
#optionally to output the full Array in each Range you could use
#(from_date..to_date).to_a
(from_date..to_date)
end
end
end
Then call as
LeaveRequest.user_requested_ranges(6,1)

logging mysql statements executed by jdbc

I tried to log mysql statements , executed by jdbc , hoping to see strings containing declare some cursor or fetch some cursor. But there was not.
Are there any ways to see them?
PS i set variables in mysql as following:
SET GLOBAL log_output="FILE"
SET GLOBAL general_log_file="Path/File"
SET GLOBAL general_log='ON'
Try this.
Add 'logger' and 'profileSQL' to the jdbc url:
&logger=com.mysql.jdbc.log.Slf4JLogger&profileSQL=true
Then you will get the SQL statement below:
2016-01-14 10:09:43 INFO MySQL - FETCH created: Thu Jan 14 10:09:43 CST 2016 duration: 1 connection: 19130945 statement: 999 resultset: 0
2016-01-14 10:09:43 INFO MySQL - QUERY created: Thu Jan 14 10:09:43 CST 2016 duration: 1 connection: 19130945 statement: 999 resultset: 0 message: SET sql_mode='NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'
2016-01-14 10:09:43 INFO MySQL - FETCH created: Thu Jan 14 10:09:43 CST 2016 duration: 1 connection: 19130945 statement: 999 resultset: 0
2016-01-14 10:09:43 INFO MySQL - QUERY created: Thu Jan 14 10:09:43 CST 2016 duration: 2 connection: 19130945 statement: 13 resultset: 17 message: select 1
2016-01-14 10:09:43 INFO MySQL - FETCH created: Thu Jan 14 10:09:43 CST 2016 duration: 0 connection: 19130945 statement: 13 resultset: 17
2016-01-14 10:09:43 INFO MySQL - QUERY created: Thu Jan 14 10:09:43 CST 2016 duration: 1 connection: 19130945 statement: 15 resultset: 18 message: select ##session.tx_read_only
2016-01-14 10:09:43 INFO MySQL - FETCH created: Thu Jan 14 10:09:43 CST 2016 duration: 0 connection: 19130945 statement: 15 resultset: 18
2016-01-14 10:09:43 INFO MySQL - QUERY created: Thu Jan 14 10:09:43 CST 2016 duration: 2 connection: 19130945 statement: 14 resultset: 0 message: update sequence set seq=seq+incr where name='demo' and seq=4602
2016-01-14 10:09:43 INFO MySQL - FETCH created: Thu Jan 14 10:09:43 CST 2016 duration: 0 connection: 19130945 statement: 14 resultset: 0
The default logger is:
com.mysql.jdbc.log.StandardLogger
Mysql jdbc property list: https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

what is the problem of this mysql query?

SELECT
*
FROM
table_temp
WHERE
install_date < NOW()
AND install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")
The problem resides on theis line:
install_date > DATE_FORMAT(2011 - 06 - 16, "%Y-%m-%d")
The 1st variable should be a string of a date
For example:
SELECT DATE_FORMAT('2007-10-04 22:23:00', '%H:%i:%s');
Or in your case:
install_date > DATE_FORMAT('2011 - 06 - 16', "%Y-%m-%d")
See MySQL DOC
The value 2011 - 06 - 16 needs to be wrapped up in quotes