Microsoft Visual Studio 2019 Execute SQL Task Editor Syntax Errors Encountered - sql

Trying to build a query on Execute SQL Task Editor.
SELECT ? = CONVERT(VARCHAR(10), COUNT(DISTINCT Email))
FROM [xxx].[dbo].[DataTEST] AS D_Test INNER JOIN
[xxx].[dbo].[ListsTEST] AS L_Test ON D_Test.ListId = L_Test.Id
WHERE L_Test.[DataSent] = 0 AND Email IS NOT NULL
When building the Query I get the error:
Error in SELECT clause: 'expression near '='.
Missing FROM clause.
Unable to parse query text.
Do I need to use '?=' for this query? Im only using that as I am emulating the .dtsx file that I am trying to diagnose/ reverse engineer.
Ive ran the query without the ?= on SQL Management Studio and it gives me the data that I want but Im unsure if me running it without that wont generate the same results.

Declare #output varchar(10) = '';
Select #output = count(Distinct Email))
From [xxx].[dbo].[DataTEST] AS D_Test
Inner Join [xxx].[dbo].[ListsTEST] AS L_Test ON D_Test.ListId = L_Test.Id
Where L_Test.[DataSent] = 0
And Email Is Not Null;
Set #output = ?;

Related

Problem with Stored Procedures in PHPmyAdmin

Attempting to perform operations with a random integer in SQL.
The following code works perfectly as intended when run as pure SQL, but triggers a syntax error when attempting to save it into a stored procedure.
SET #sample_count = (SELECT count(*)
FROM cinder_sample);
SELECT #sample_count;
SET #random_ID = (SELECT FLOOR(RAND()*#sample_count));
SELECT #random_ID;
Any ideas as to what could be going wrong?
The exact error triggered is:
"The following query has failed: "CREATE DEFINER=root#localhost PROCEDURE play_random_sp() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER DELIMITER // SET #sample_count = (SELECT count() FROM cinder_sample)// SELECT #sample_count// SET #random_ID = (SELECT FLOOR(RAND()#sample_count))// SELECT #random_ID"
MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '// SET #sample_count = (SELECT count(*) FROM cinder_sample)// SELECT' at line 1"

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

How can I pass an int into the OPEN QUERY using a stored procedure in a linked server?

I am trying to create a stored procedure using an OPEN QUERY for a linked server. I am having a problem on the Where Clause.
I tried converting the where clause for the user id into varchar(10)
DECLARE #user_id as bigint = 32
SELECT * FROM OPENQUERY(LMSBUILDER,
'SELECT
cc.id,cc.user_id,cc.title,cc.library_id
,cc.parameters,cc.filtered,cc.embed_type
,ll.name, ll.title as lib_title, ll.major_version
,ll.minor_version,ll.patch_version,ll.runnable
,ll.restricted,ll.fullscreen,ll.embed_types
, ll.semantics
FROM
coursebuilder.wp_h5p_contents as cc
INNER JOIN coursebuilder.wp_h5p_libraries as ll
ON cc.library_id=ll.id
WHERE
cc.library_id IN (35,22, 34)
AND cc.user_id = '' + CONVERT(NVARCHAR(10), #user_id) + ''')
It should return all the results but instead I only got a null. Whenever I try to run it in mysql it returns values
This duplicate link is resolve your answer.
Microsoft Support How to pass a variable to a linked server query is also resolve your issue.

Oracle SQL update from one Table to another table throws syntax error

Oracle SQL update from one Table to another table throws syntax error for following simple update query.
UPDATE Sales_Import SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID;
Error:
Error starting at line 1 in command:
Error at Command Line:2 Column:37
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
I believe the same query will work in other databases like postgres etc.
Could any one tell the correct query ?
and whatever i tried is this ANSI standard query ?
and whatever i tried is this ANSI standard query ?
No. Oracle Oracle doesn't support join in update statement.
In Oracle you could do it in two ways -
**Merge statement **
with only update clause in merge statement
MERGE INTO sales_import s
USING (SELECT *
FROM retrieveaccountnumber) u
ON (u.leadid = s.leadid)
WHEN matched THEN
UPDATE SET u.accountnumber = s.accountnumber;
Correlated query
UPDATE sales_import t1
SET accountnumber = (SELECT t2.accountnumber
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid )
WHERE EXISTS (
SELECT 1
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid );
I will write your sql like this:
UPDATE Sales_Import SI
SET AccountNumber = (Select RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID);
You can do this by joining those tables:
UPDATE SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
JOIN Sales_Import SI ON RAN.LeadID = SI.LeadID;

SSIS 2008 how to use EXEC

Im using SSIS 2008
i want to try and run the following SQL code:
declare #test nvarchar(100)
set #test = 'Select Distinct Area From dbo.udf_RiskManagementPlan('') Where Area is not Null And Area != ''';
if exists (select object_id from sys.columns where object_id = object_id('dbo.udf_RiskManagementPlan') and name = 'Area')
exec sp_executesql #test
Else select Null As Area
It is working fine in SQL managment studio, but when i try and place it in OLEDB source i keep getting error regarding syntax
i tried placing the entire code in a variable value and just call that still gave same syntax error
i have feeling this has more to do with SSIS problem handling stored procedure and parameters then the syntax, but i cant seem find way around it
Actually in SSIS you can't put such expression in source. Split it into parts:
Put an SQL Task block with EXEC dbo.udf_RiskManagementPlan, set input/output parameters to get result (e.g. #SQLTaskResult)
On positive finish: set a variable Source_SQL to:
DTS.Variables["Source_SQL"].Value = 'Select Distinct Area From
'+DTS.Variables["SQLTaskResult"].Value+' Where Area is not Null And Area != ''
On error /no procedure or other error/ set Source_SQL to Else select Null As Area similar way. Double click the right connection, and set Value to Falure
Proceed to Data Flow - set OLE DB to read source sql dynamically from a variable in OLE DB Source Editor