Variable as SQL in cfquery - sql-server

I have a table that is storing SQL as one of its elements. I need to be able to run that stored SQL query, but can't figure out how to do it. So far, I'm using the following method:
<cfquery name="qDynamicTag" datasource="#myDSN#">
#PreserveSingleQuotes(arguments.sql)#
</cfquery>
If I dump the value of #PreserveSingleQuotes(arguments.sql)# before executing the cfquery I see the correct SQL statement. However, when I try to run the above cfquery I get a SQLException with the message:
Syntax error at token 0, line 0 offset 0
An example of one of the SQL statements that I'm trying to run:
select productid from catalog.producttag where tag = 'case' or tag = 'cases'
Any ideas on what I'm doing wrong?

Ha! Thanks for all your comments. The suggestion to check the debugging output of the DB activity made me realize that I wasn't seeing the DB activity in the debug section of the page because the query was executing through an AJAX call. After calling the page with the query directly, the DB debug output revealed that the code was good, but the data in the database was bad. One of the records with SQL didn't exist. Thus, cfquery was trying to run a blank string.

Related

How to get rid of SQL Server Invalid Object name error?

I'm trying to insert some data into a table within SQL Server.
Below is the query that I use, but I get an error
'Invalid Object Name'
in SQL Server Management Studio.
The table does exist within the list of tables, and my database is set to 'BC-TEST' as well.
When I type the exact same query again, it works:
Done some research, and a lot of posts are referring to either caching or the database that is not set to the correct one. However none of these seems to be the case here.
Can someone help me out?
Cheers!
Both are two different queries, they are not the exact same.
The first query insert into [...$packaging processing], while the other one, the second query insert into [....$packing processing]
if the second query works perfectly, then the correct table must be [....$packing processing]

An item with the same key has already been added-No duplicate column in query

When I run the SSRS report in SharePoint it runs fine, but when I try to execute it in the development environment, it results in a blank report. No data is displaying.
And also when I am trying to refresh the fields of the dataset used in this report, I am unable to refresh. It is showing the error "could not update a list of fields for the query. Verify that you can connect to the data source and that your query syntax is correct" and when the details is showing as "An item with the same key has already been added"
I have checked for my datasource connection and it is fine and I could not find anything wrong with my SQL query also. It is giving correct result when I am running it as query. And I also checked in query that I have used no column as duplicate while using alias.
Probably there is some kind of duplicate column name, try to put your whole dataset query as subquery
example: select * from ( [query from ssrs dataset] )ssrs.
Execute such statement in mssql and see if there is an error.

SSDT/SSIS Execute SQL Task not populating variable with query output

My Execute SQL Task uses a simple query that should be returning a single value. I want to write that value to a variable so that I can use it in a data flow task later but the result never seems to get written to my variable.
The SQL statement is simple:
select max (rec_id) from [dbo].[RX_BILLING_TEST]
and I believe that I've set up the task correctly:
When I execute the task, it completes successfully but the variables window shows that the value of my variable didn't update. I set breakpoints on variable value changed and on post execute but that didn't help.
As much as I hate errors, this is a case where I would appreciate getting one to point me in some direction. Any ideas what might be wrong?
In a similar case there are many things you have to check:
Try adding an alias to the aggregate function
select max (rec_id) AS Max_Rec_Id from [dbo].[RX_BILLING_TEST]
Check that your package doesn't have more than one variable User::v_Recid with different scopes. or that User::v_Recid scope's is Execute SQL Task. (it must be the Package)
Test your SQL Command using Sql Server Management Studio
Try adding the database name to your Command
select max (rec_id) AS Max_Rec_Id from [MyDB].[dbo].[RX_BILLING_TEST]

Inserting Data into SQL Server from variables via SSIS

I have consumed simple web service for addition of two numbers and I am getting the result in xml format that I have stored in variable supposed named as my_data, now I have to insert that result in my sql table named as Data_result by writing query in oledb source editor, but unable to insert data using variable.I just want to store all the result calculated by my web service in sql table.
Since I am newbie to SSIS, any help is greatly appreciated
When I am trying to insert the data by the following query command:
"INSERT INTO Data_Result(Result) SELECT '"+#USER::my_data+"'"
It's giving error:
Error at Data Flow Task [OLE DB Source [55]]: No Column Transformation was return by SQL Command
--------Updated explanation of errors based on the solution given
Error for executing query
If I place the expression without the "" quotes then I get following error
If I place the query in double quotes then following error is shown
And if I remove User from User::Data Variable and place the query in double quotes then I get following screen
although the expression evaluates but after proceeding further on this evaluated expression when I am trying to search for the variable in expression column of Execute sql Task, then I am unable to locate the newly created variable, as shown below
------------Updated question according to other query----------
Here is the picture of my whole work flow
This is what I have did inside the for each loop container under collection tab
And this below setting I have done between Variable mapping tab,
And in below screen shot, I am using Execute SQL Task to enter my data obtained from web service task into database using an insert query, but unable to fire proper insert query,
And below is my XML file,
<?xml version="1.0" encoding="utf-16"?>
Reshma
1988-09-23T00:00:00
,
This name and birthdate I received from web service I want to insert that into database table
--------------Updated question for foreach loop container--------
Below is the foreach loop container I am using,
But still I am not getting node value into variable and also I am suppose to make use of object type variable or it can work with string type of variable.
I suggest you build this SQL statement in another variable, as explained here:
http://consultingblogs.emc.com/jamiethomson/archive/2005/12/09/2480.aspx
Then use that variable in your execute SQL statement
Normally you would try and parameterise the query but in this case (as in many others) it won't work.
Here are some more detailed instructions:
A. Generate the dynamic SQL
Create a new SSIS variable of type string
In properties, set it's 'Evaluate as Expression' to TRUE
In properties, click on 'Expression' and build an expression as follows:
"INSERT INTO Data_Result(Result) VALUES ('"+#[USER::my_data]+"')"
Press Evaluate and verify that the output is valid SQL (you may even wish to paste it into SSMS and test)
B. Get an Execute SQL task to run your SQL:
Drop a execute SQL task on your page. Set it's connection
With the execute SQL task highlighted, go to properties and click expressions
Pick the property 'SQLStatementSource'. In the expression pick your variable that you built in step A and press evaluate.
Press OK on everything
Now whatever change occurs in my_data will automatically be applied to your execute SQL task
Try that for starters and get back to me.

string or binary data would be truncated - but don't know parameter values

The company program prints out the string or binary data would be truncated error to an error table when trying to insert into a specific table. I do realize from other posts that the cause of this is the current table structure. There are one or more fields that are too short, but unfortunately I do not have access to the values of the actual query. Instead, it dumps them as #parametername1, #parametername2, etc. from the stacktrace.
Is there any way I can see from some kind of monitoring tool in SQL Server 2012 what parameter and value failed? I mean, the SQL Server has returned the error, so supposedly I can log it if I repeat the error?
You can setup a trace in SQL Profiler. For the statement, it will give you list of columns and matching values being inserted.

Resources