Translate := from MySQL to SQL Server 2014 - mysql

I had several troubles when translating a stored procedure from MySQL to SQL Server 2014 with the assign operator :=.
What solution is suggested?

Check the following link: http://dev.mysql.com/doc/refman/5.7/en/assignment-operators.html#operator_assign-value
You can use = in your T-SQL script instead.

Related

SQL syntax error has occurred

Here is my simple query:
my $SQLp = "SELECT MAX([PawnPayments].[CreationTimeDate]) as MaxTransDate
FROM [PawnSafeDBCE].[dbo].[PawnPayments]
INNER JOIN [PawnSafeDBCE].[dbo].[PawnPaymentDetails]
ON [PawnPayments[.[PaymentID] = [PawnPaymentDetails].[PaymentID]
WHERE [PawnPaymentDetails].[TicketID[ = '$TicketID'
AND [PawnPaymentDetails].[StoreID] ='$StoreID'
Note that query is written on Perl engine. I keep receiving an error that says:
"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 '[PawnPayments].[CreationTimeDate]) as MaxTransDate:"
I believe the error has to do with the bracket notation, but unfortunately, I am having to use this style due to a poorly constructed 3rd party table. Any help? Am I missing something obvious?
Huge EDIT: The table I am querying is actually on a SQL server, not a MySQL server! My database runs on the MySQL server, but this 3rd party database runs on an older version of Microsoft SQL.
I don't know why you have all those square brackets around your table and column names, but they aren't necessary and they aren't standard SQL. That's what is causing your syntax error.
my $SQLp = "SELECT MAX(PawnPayments.CreationTimeDate) as MaxTransDate
FROM PawnSafeDBCE.dbo.PawnPayments
INNER JOIN PawnSafeDBCE.dbo.PawnPaymentDetails
ON PawnPayments.PaymentID = PawnPaymentDetails.PaymentID
WHERE PawnPaymentDetails.TicketID = '$TicketID'
AND PawnPaymentDetails.StoreID ='$StoreID'";
I'll also add that having variables interpolated in your SQL statement like that is potentially leaving you open to SQL injection attacks. Far better to use bind points in your SQL and use extra arguments to execute to fill in the values (assuming you're using DBI).
my $SQLp = "SELECT MAX(PawnPayments.CreationTimeDate) as MaxTransDate
FROM PawnSafeDBCE.dbo.PawnPayments
INNER JOIN PawnSafeDBCE.dbo.PawnPaymentDetails
ON PawnPayments.PaymentID = PawnPaymentDetails.PaymentID
WHERE PawnPaymentDetails.TicketID = ?
AND PawnPaymentDetails.StoreID = ?";
my $sth = $dbh->prepare($SQLp);
$sth->execute($TicketID, $StoreID);
Update: As Bill Karwin points out in a comment, the database.schema.table syntax makes no sense in a MySQL database. So I think you're a little confused. The error message you are getting definitely mentions MySQL, so you're connecting to a MySQL server, using DBD::MySQL - but perhaps you should be connecting to an MSSQL server instead.
It might be useful if you showed us your database connection code - the call that sets up your $dbh (or equivalent) variable.
You say you are querying a MS SQL database, but the error message clearly says you are using a MySQL database or a MySQL database driver.
If you are querying a MS SQL database, fix your connection string.
If you are querying a MySQL database, use a MySQL-compatible query. MySQL uses backticks to quote identifiers (not square brackets like MS SQL).
[PawnPayments].[CreationTimeDate]
should be
`PawnPayments`.`CreationTimeDate`
Note that your code suffers from injection bugs due to incorrect quoting of value inserted into the SQL query. (It's not good enough just to put quotes around the values!) These can cause your code to fail, and they could make you vulnerable to injection attacks. Fix the quoting, or use replaceable parameters.

Stored procedure execution in Entity Framework in SQL Server 2005

I am using MVC3 and Entity Framework for calling a stored procedure in SQL Server 2008. The procedure has no parameters and in the profiler I get the proc execution as
exec sp_executesql N'sp_GetDashSessionboardRoomTimeSlot', N'#p0 nvarchar(4000)', #p0=NULL
This is how I call in the c# code.
SqlParameter sa = null;
var query = from dashboardData in TBSCIDBContext.Database.SqlQuery<SessionDashboardData>("sp_GetDashSessionboardRoomTimeSlot", sa)
select dashboardData;
This works fine. But I have changed my database to SQL Server 2005 and I need to use it
So when I call the same procedure using the same C# code the profiler returns the same exec statement but the sql gives error
Incorrect syntax near 'sp_GetDashSessionboardRoomTimeSlot'
pls help me in how to fix this problem as I need to use this stored procedure and in SQL Server 2005
changed it to
var query = from dashboardData in TBSCIDBContext.Database.SqlQuery("sp_GetDashSessionboardRoomTimeSlot")
select dashboardData;
it works
Change the ProviderManifestToken in SSDL (in your EDMX) to 2005. You probably have it set to 2008.

Migration from SQL Server 2008 to Azure

I'm using SQLAzureMW v3.8.8 but I get lots of errors in the script generated. And the problem is that i don't know in which line is each error generated.
Error #: 105 -- Unclosed quotation mark after the character string
'CREATE PROCEDURE [dbo].[spAdminParametrosGet]
Error #: 156 -- Incorrect syntax near the keyword 'ELSE'.
Error #: 40512 -- Deprecated feature 'NOLOCK or READUNCOMMITTED in
UPDATE or DELETE' is not supported in this version of SQL Server.
Incorrect syntax near '
The TSQL script generates sql stored procedures as strings and are created using dynamic SQL. Some stored procedures have comments inside of it.
May that be the cause or any suggestion to quickly migrate the database to Azure?
It is very much possible that some of the SP and other statements which you want to migrate from SQL Server to SQL Azure are not compatible. Here is a list of supported and unsupported TSQL features:
http://msdn.microsoft.com/en-us/library/windowsazure/ee336250.aspx
Also you haven't mentioned what is your source SQL Server is? As not all SQL Server will full features are supported by SAMQ (3.8 or 4.01)
Also please match your TSQL statements from the unsupported list below and check if any of listed below are part of your TSQL statements:
http://msdn.microsoft.com/en-us/library/windowsazure/ee336253.aspx

SQL Server 2008 get data from unified tables

I've a stored proc like:
CREATE PROCEDURE [dbo].[GetData]
#code varchar(10)
AS
BEGIN
SET NOCOUNT ON;
--code goes here
END
the proc reads data from one of n tables, based on #code passed. So I've a map linking codes with the actual table names, ex.
Code:"A" => dbo.JAN_SALES
Code:"B"=> dbo.FEB_SALES
All tables have the same structure. I know how to get it done by using 'red' sql, wonder if there's more elegant/performant way of doing that with SQL Server 2008?
Edit 1 - Red sql is the sql, which gets built by concatenating its parts and executed by calling something like exec('select A. B, C from ' + #myTable).
Your question seems to be clear on what needs to be done, and there are no much more posibilities than creating the T-SQL code by
1) adding IF blocks evaluating the #code parameter; or
2) using Dynamic Sql ( dynamic sql = "red" ). Please note the dynamic sql is strongly discouraged for production environments.
BTW - On SQL Server 2008 you can use *sp_executesql* proprietary stored procedure. Microsoft's MSDN describes how it works here.

Getting SQL stored procedure results into data.frame format using RODBC

I am using the RODBC package to query for results in my SQL server. I have a certain stored procedure written that, when executed in my SQL Server Mgmt. studio (for example), returns a table. However, when I run the query through R, it returns character(0)
# Execute command...
sqlQuery(production,"exec port.tdp_RISK2_ModelRunCompare #ModelRunId1 = 399")
Weird thing is... when I do something like...
sqlQuery(production,"exec sp_who")
I get a table of results...
Help?
I had the same problem.
You can try using:
set nocount on
in the MS SQL Server stored procedure so it will return just a dataset.
Regards,
Try this:
sqlQuery(production,"exec port.tdp_RISK2_ModelRunCompare #ModelRunId1 = 399", errors=FALSE)
Also try writing to a new data frame. I kept getting character(0) when I tried to overwrite an existing data frame with the sqlQuery function.