Converting MSSQL function to MySQL function - mysql

I am trying to convert this user defined function (taken from a MSSQL) and tweak it so that I can run it MYSQL. I have made several attempts but all seem to error on the declare variable.
I am running the following version: 5.6.11-log - MySQL Community Server (GPL)
USE [DataGB]
GO
/****** Object: UserDefinedFunction [dbo].[FullPostCodeFix] Script Date: 11/20/2013 16:10:44 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
CREATE FUNCTION [dbo].[FullPostCodeFix] (#Postcode VARCHAR(20))
RETURNS VARCHAR(20)
BEGIN
/*
Puts postcode into correct format if it is currently in any of the below formats
AB12AA
AB 12AA
AB1 2AA
AB 1 2AA
Returns #Postcode
*/
DECL ARE #District Varchar(50)
DECLARE #Remainder Varchar(50)
DECLARE #Sector Varchar(50)
SET #District= CASE
WHEN LEN(#Postcode) - CHARINDEX(' ', REVERSE(#Postcode)) = len(#Postcode) THEN SUBSTRING(#Postcode,1,(len(#Postcode) - 3))
WHEN LEN(#Postcode) - CHARINDEX(' ', REVERSE(#Postcode)) < 3 THEN SUBSTRING(#Postcode,1,(len(#Postcode) - 3))
ELSE SUBSTRING(#Postcode, 0, LEN(#Postcode) - CHARINDEX(' ', REVERSE(#Postcode)) + 1)
END
SET #District = dbo.PostcodeFix(#District)
SET #Remainder= RIGHT(#Postcode,3)
SET #Sector = #District + ' ' + LEFT(#Remainder,1)
SET #Postcode = #District + ' ' + #Remainder
RETURN #Postcode
END
My attempt at creating a MYSQL version is below:
CREATE FUNCTION FullPostCodeFix (Postcode VARCHAR(20))
RETURNS VARCHAR(20)
BEGIN
DECLARE District Varchar(50)
DECLARE Remainder Varchar(50)
DECLARE Sector Varchar(50)
SET District= CASE
WHEN LEN(Postcode) - CHARINDEX(' ', REVERSE(Postcode)) = len(Postcode) THEN SUBSTRING(Postcode,1,(len(Postcode) - 3))
WHEN LEN(Postcode) - CHARINDEX(' ', REVERSE(Postcode)) < 3 THEN SUBSTRING(Postcode,1,(len(Postcode) - 3))
ELSE SUBSTRING(Postcode, 0, LEN(Postcode) - CHARINDEX(' ', REVERSE(Postcode)) + 1)
END
SET District = dbo.PostcodeFix(District)
SET Remainder= RIGHT(Postcode,3)
SET Sector = District + ' ' + LEFT(Remainder,1)
SET Postcode = District + ' ' + Remainder
RETURN Postcode
END
The error that I get is as follows:
Error Code: 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 'DECLARE Remainder Varchar(50) DECLARE Sector Varchar(50)
There is another function called from within the function "FullPostCodeFix". This is my attempt:
DELIMITER $$
CREATE FUNCTION PostCodeFix (strDistrict VARCHAR(20))
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE intASCII INTEGER;
SET strDistrict = LTRIM(strDistrict);
SET strDistrict = RTRIM(strDistrict);
IF LENGTH(strDistrict) > 4 OR LENGTH(strDistrict) = 0 THEN RETURN 'ERROR: ' + strDistrict;
ELSE
BEGIN
SET intASCII = ASCII(LEFT(strDistrict, 1));
IF ( intASCII > 47 AND intASCII < 58 ) THEN RETURN 'ERROR: ' + strDistrict;
ELSE
BEGIN
SET intASCII = ASCII(SUBSTRING(strDistrict, 2, 1));
IF ( intASCII > 47 AND intASCII < 58 ) THEN SET strDistrict = LEFT(strDistrict, 1) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-1);
SET intASCII = ASCII(SUBSTRING(strDistrict, 3, 1));
IF ( intASCII < 48 OR intASCII > 57 ) AND (intASCII <> 32) THEN RETURN 'ERROR: ' + strDistrict;
ELSE IF LENGTH(strDistrict) < 4 THEN SET strDistrict = LEFT(strDistrict, 2) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-2);
END IF;
END IF;
RETURN strDistrict;
END IF;

Ok lets start, replace MS-SQL functions with MySQL equivalent functions.
MSSQL MySQL
LEN() LENGTH()
SUBTRING() SUBSTR()
CHARINDEX() INSTR()
below is documentation and list of all MySQL String functions
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Here is correct MySQL Syntax that I have verified.
CREATE FUNCTION FullPostCodeFix (Postcode VARCHAR(20)) RETURNS VARCHAR(20)
BEGIN
DECLARE district VARCHAR(50);
DECLARE remainder VARCHAR(50);
DECLARE sector VARCHAR(50);
IF LENGTH(Postcode) - INSTR(' ', REVERSE(Postcode)) = LENGTH(Postcode) THEN SET district = SUBSTR(Postcode,1,(LENGTH(Postcode) - 3));
ELSEIF LENGTH(Postcode) - INSTR(' ', REVERSE(Postcode)) < 3 THEN SET district = SUBSTR(Postcode,1,(LENGTH(Postcode) - 3));
ELSE SET district = SUBSTR(Postcode, 0, LENGTH(Postcode) - INSTR(' ', REVERSE(Postcode)) + 1);
END IF;
SET District = dbo.PostcodeFix(District);
SET Remainder= RIGHT(Postcode,3);
SET Sector = CONCAT(District,' ',LEFT(Remainder,1));
SET Postcode = CONCAT(District,' ',Remainder);
RETURN Postcode;
END
Here is your second function
CREATE FUNCTION PostCodeFix (strDistrict VARCHAR(20))
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE intASCII INTEGER;
SET strDistrict = LTRIM(strDistrict);
SET strDistrict = RTRIM(strDistrict);
IF LENGTH(strDistrict) > 4 OR LENGTH(strDistrict) = 0 THEN RETURN 'ERROR: ' + strDistrict;
ELSE
SET intASCII = ASCII(LEFT(strDistrict, 1));
IF ( intASCII > 47 AND intASCII < 58 ) THEN RETURN 'ERROR: ' + strDistrict;
ELSE
SET intASCII = ASCII(SUBSTRING(strDistrict, 2, 1));
IF ( intASCII > 47 AND intASCII < 58 ) THEN SET strDistrict = LEFT(strDistrict, 1) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-1);
END IF;
SET intASCII = ASCII(SUBSTRING(strDistrict, 3, 1));
IF ( intASCII < 48 OR intASCII > 57 ) AND (intASCII <> 32) THEN RETURN 'ERROR: ' + strDistrict;
ELSEIF LENGTH(strDistrict) < 4 THEN SET strDistrict = LEFT(strDistrict, 2) + ' ' + RIGHT(strDistrict, LENGTH(strDistrict)-2);
END IF;
END IF;
RETURN strDistrict;
END IF;
END

Related

Transfer login from SQL2014 to SQL2008R2

I use a stored procedure called sp_help_revlogin to transfer my SQL2014 logins to SQL2008R2.
I usually use it to transfer logins between SQL2008R2 and it works fine but today i have to transfer 2 logins from 2014 to 2008R2 and i receive this error message :
Msg 15021, Level 16, State 2, Line 2 Invalid value given for parameter
PASSWORD. Specify a valid parameter value.
sp_help_revlogin content ==>
USE [master]
GO
/****** Object: StoredProcedure [dbo].[sp_help_revlogin] Script Date: 31/08/2016 11:38:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_help_revlogin] #login_name sysname = NULL AS
DECLARE #name sysname
DECLARE #type varchar (1)
DECLARE #hasaccess int
DECLARE #denylogin int
DECLARE #is_disabled int
DECLARE #PWD_varbinary varbinary (256)
DECLARE #PWD_string varchar (514)
DECLARE #SID_varbinary varbinary (85)
DECLARE #SID_string varchar (514)
DECLARE #tmpstr varchar (1024)
DECLARE #is_policy_checked varchar (3)
DECLARE #is_expiration_checked varchar (3)
DECLARE #defaultdb sysname
IF (#login_name IS NULL)
DECLARE login_curs CURSOR FOR
SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name <> 'sa'
ELSE
DECLARE login_curs CURSOR FOR
SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = #login_name
OPEN login_curs
FETCH NEXT FROM login_curs INTO #SID_varbinary, #name, #type, #is_disabled, #defaultdb, #hasaccess, #denylogin
IF (##fetch_status = -1)
BEGIN
PRINT 'No login(s) found.'
CLOSE login_curs
DEALLOCATE login_curs
RETURN -1
END
SET #tmpstr = '/* sp_help_revlogin script '
PRINT #tmpstr
SET #tmpstr = '** Generated ' + CONVERT (varchar, GETDATE()) + ' on ' + ##SERVERNAME + ' */'
PRINT #tmpstr
PRINT ''
WHILE (##fetch_status <> -1)
BEGIN
IF (##fetch_status <> -2)
BEGIN
PRINT ''
SET #tmpstr = '-- Login: ' + #name
PRINT #tmpstr
IF (#type IN ( 'G', 'U'))
BEGIN -- NT authenticated account/group
SET #tmpstr = 'CREATE LOGIN ' + QUOTENAME( #name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + #defaultdb + ']'
END
ELSE BEGIN -- SQL Server authentication
-- obtain password and sid
SET #PWD_varbinary = CAST( LOGINPROPERTY( #name, 'PasswordHash' ) AS varbinary (256) )
EXEC sp_hexadecimal #PWD_varbinary, #PWD_string OUT
EXEC sp_hexadecimal #SID_varbinary,#SID_string OUT
-- obtain password policy state
SELECT #is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = #name
SELECT #is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = #name
SET #tmpstr = 'CREATE LOGIN ' + QUOTENAME( #name ) + ' WITH PASSWORD = ' + #PWD_string + ' HASHED, SID = ' + #SID_string + ', DEFAULT_DATABASE = [' + #defaultdb + ']'
IF ( #is_policy_checked IS NOT NULL )
BEGIN
SET #tmpstr = #tmpstr + ', CHECK_POLICY = ' + #is_policy_checked
END
IF ( #is_expiration_checked IS NOT NULL )
BEGIN
SET #tmpstr = #tmpstr + ', CHECK_EXPIRATION = ' + #is_expiration_checked
END
END
IF (#denylogin = 1)
BEGIN -- login is denied access
SET #tmpstr = #tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( #name )
END
ELSE IF (#hasaccess = 0)
BEGIN -- login exists but does not have access
SET #tmpstr = #tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( #name )
END
IF (#is_disabled = 1)
BEGIN -- login is disabled
SET #tmpstr = #tmpstr + '; ALTER LOGIN ' + QUOTENAME( #name ) + ' DISABLE'
END
PRINT #tmpstr
END
FETCH NEXT FROM login_curs INTO #SID_varbinary, #name, #type, #is_disabled, #defaultdb, #hasaccess, #denylogin
END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0
someone can help me please ?
THanks a lot.
Ludo

MySQL convert Degree, Minutes, Seconds to Degree decimal

I have multiple rows of Degrees Minutes Seconds that I need to convert with a query.
36°19'11.46" N = 36.31985
95°36'02.22" W = 95.600617
Each row is going to be different. I've been stuck on this for two days. Is this even possible?
Nice lifehack: reverse problem solution (degree to DMS) using SEC_TO_TIME built-in MySQL function:
CREATE FUNCTION `geocoords`(lon double, lat double) RETURNS varchar(24) CHARSET cp1251
NO SQL
DETERMINISTIC
begin
declare alon double;
declare alat double;
declare slon varchar(12);
declare slat varchar(12);
set alon = abs(lon);
set alat = abs(lat);
set slon = TIME_FORMAT(SEC_TO_TIME(alon*3600), '%H°%i''%s"');
set slat = TIME_FORMAT(SEC_TO_TIME(alat*3600), '%H°%i''%s"');
if lon>0 then
set slon = concat(slon, 'E');
elseif lon<0 then
set slon = concat(slon, 'W');
end if;
if lat>0 then
set slat = concat(slat, 'N');
elseif lat<0 then
set slat = concat(slat, 'S');
end if;
return concat(slat, ' ', slon);
end
SELECT geocoords(30.550157546997, 50.344024658203)
50°20'38"N 30°33'01"E
The following should work:
SELECT D + M/60 + S/3600;
For example, in MySQL:
SELECT 36 + 19/60 + 11.46/3600;
returns: 36.319850
I ended up building this, and it worked flawlessly with what I needed. You will note that I added a C to the numbers, this is to flag them, so if it had already been converted it wouldn't continue to convert.
UPDATE
MasterTable
SET
MasterTable.Latitude_A = MasterTable.Latitude,
MasterTable.Longitude_A = MasterTable.Longitude
WHERE
ProjectID = 'ProjectAlpha'
and Sequence = '0'
and MasterTable.Latitude NOT LIKE '%C%'
and MasterTable.Longitude NOT LIKE '%C%';
TRUNCATE TABLE gpsconvert;
INSERT into gpsconvert(gpsconvert.`Account Number`,gpsconvert.Latitude,gpsconvert.Longitude)
SELECT
MasterTable.AccountNumber,
MasterTable.Latitude,
MasterTable.Longitude
FROM
MasterTable
WHERE
MasterTable.ProjectID = 'ProjectAlpha'
and MasterTable.Sequence = '0'
and MasterTable.Latitude NOT LIKE '%c%'
and MasterTable.Longitude NOT LIKE '%c%'
and MasterTable.Latitude <> ''
and MasterTable.Longitude <> '';
UPDATE
gpsconvert
SET
gpsconvert.LatDegree = LEFT(gpsconvert.Latitude,2),
gpsconvert.LatMinutes = SUBSTRING(gpsconvert.Latitude,-7,2),
gpsconvert.LatSeconds = SUBSTRING(gpsconvert.latitude,-5,5),
gpsconvert.LatDecimal = gpsconvert.LatDegree + (gpsconvert.LatMinutes/60) + (gpsconvert.LatSeconds/3600),
gpsconvert.LongDegree = LEFT(gpsconvert.Longitude,2),
gpsconvert.LongMinutes = SUBSTRING(gpsconvert.Longitude,-7,2),
gpsconvert.LongSeconds = SUBSTRING(gpsconvert.Longitude,-5,5),
gpsconvert.LongDecimal = gpsconvert.LongDegree + (gpsconvert.LongMinutes/60) + (gpsconvert.LongSeconds/3600);
UPDATE
MasterTable
INNER JOIN
gpsconvert on gpsconvert.`Account Number` = MasterTable.AccountNumber
SET
MasterTable.Latitude = CONCAT(gpsconvert.LatDecimal,'c'),
MasterTable.Longitude = CONCAT(gpsconvert.LongDecimal,'c')
WHERE
MasterTable.ProjectID = 'ProjectAlpha'
and MasterTable.Sequence = '0'
and MasterTable.AccountNumber = gpsconvert.`Account Number`
This converts strings in the form 21°48'6.384" to decimal
CREATE FUNCTION database.DMS_TO_DECIMAL(dms varchar(20))
RETURNS decimal(12,9)
BEGIN
DECLARE deg decimal(12,9);
DECLARE mins decimal(12,9);
DECLARE secs decimal(12,9);
SET deg = CAST( SUBSTRING_INDEX(dms, '°', 1) AS decimal(12,9));
SET mins = CAST( (SUBSTR(dms, POSITION('°' IN dms) + 1, POSITION("'" IN dms) - POSITION('°' IN dms) - 1)) AS decimal(12,9));
SET secs = CAST( (SUBSTR(dms, POSITION("'" IN dms) + 1, POSITION("\"" IN dms) - POSITION("'" IN dms) - 1)) AS decimal(12,9));
RETURN deg + mins/60 + secs/3600;
END
Add orientation (W/S):
DROP FUNCTION IF EXISTS `DmsToDecimal`;
DELIMITER $$
CREATE FUNCTION `DmsToDecimal`(`dms` VARCHAR(50))
RETURNS DECIMAL(12,9)
DETERMINISTIC
NO SQL
SQL SECURITY INVOKER
BEGIN
DECLARE deg decimal(12,9);
DECLARE mins decimal(12,9);
DECLARE secs decimal(12,9);
DECLARE sign integer;
SET dms = UPPER(TRIM(REPLACE(dms, ",", ".")));
SET deg = CAST( SUBSTRING_INDEX(dms, '°', 1) AS decimal(12,9));
SET mins = CAST( (SUBSTR(dms, POSITION('°' IN dms) + 1, POSITION("'" IN dms) - POSITION("°" IN dms) - 1)) AS decimal(12,9));
SET secs = CAST( (SUBSTR(dms, POSITION("'" IN dms) + 1, POSITION("""" IN dms) - POSITION("'" IN dms) - 1)) AS decimal(12,9));
SET sign = 1 - 2 * (RIGHT(dms, 1) = "W" OR RIGHT(dms, 1) = "S";
RETURN sign * (deg + mins / 60 + secs / 3600);
END$$
DELIMITER ;

Mysql : Not allowed to return a result set from a function

I have write one function but getting this error Not allowed to return a result set from a function
DELIMITER $$
CREATE FUNCTION getTestFunction
(
p_ParentID int,
p_ListName nvarchar(50),
p_Type nvarchar(50),
p_Count int
)
RETURNS nvarchar(2000)
BEGIN
DECLARE p_KeyValue nvarchar(2000);
DECLARE p_ListValue nvarchar(2000);
DECLARE p_TextValue nvarchar(2000);
DECLARE p_ReturnValue nvarchar(2000);
DECLARE p_Key nvarchar(2000);
IF p_ParentID = 0 THEN
IF p_Count = 0 THEN
SET p_ReturnValue = '';
ELSE
SET p_ReturnValue = p_ListName;
END IF;
ELSE
SELECT p_KeyValue = ListName + '.' + Value
FROM ListsTable
WHERE EntryID = p_ParentID LIMIT 1 ;
RETURN p_ReturnValue;
If p_Type = 'ParentKey' Or (p_Type = 'ParentList' AND p_Count > 0) THEN
SET p_ReturnValue = p_KeyValue;
ELSE
IF p_Type = 'ParentList' THEN
SET p_ReturnValue = p_ListValue;
ELSE
SET p_ReturnValue = p_TextValue;
END IF;
END IF;
IF p_Count > 0 THEN
If p_Count = 1 AND p_Type = 'ParentList' THEN
SET p_ReturnValue = p_ReturnValue + ':' + p_ListName;
ELSE
SET p_ReturnValue = p_ReturnValue + '.' + p_ListName;
END IF;
END IF;
END IF;
RETURN p_ReturnValue;
END$$
DELIMITER ;
You want to assign the result of a query to a variable, but in fact you're just selecting. That's why MySQL's complaining.
You have to change this
SELECT p_KeyValue = ListName + '.' + Value
FROM ListsTable
WHERE EntryID = p_ParentID LIMIT 1 ;
to
SELECT CONCAT(ListName, '.', `Value`)
INTO p_KeyValue
FROM ListsTable
WHERE EntryID = p_ParentID LIMIT 1 ;
And you should add an ORDER BY. A LIMIT without ORDER BY doesn't make sense, since there's no guaranteed order in a relational database.
Mysql complains about SELECT statement in your function,
probably it understands SELECT p_KeyValue = ListName + '.' + Value as comparison
change it to
SELECT CONCAT(ListName, '.', Value) INTO p_KeyValue

How to get all the transaction logs (insert update delete) for a specific table in SQL Server 2008

I want to get all the transactions applied on a specific table in SQL Server 2008.
I found the last time a table was updated using this script:
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'DBName')
AND OBJECT_ID=OBJECT_ID('tableName')
I want to know all the transactions (Inserts, Updates, Deletes) for that table, and their datetime, and the query applied.
What is the best way to do this?
The only way to do this in a reasonable amount of time is to use a third party tool(as Martin said in first comment) such as ApexSQL Log that can read transaction log and get the information you need.
Note that in order for this to work your database has to be in a full recovery mode because that’s when SQL Server logs full transaction details that can be reconstructed later.
Another option is to investigate how to use undocumented fn_dblog function but this will take you a lot more time and you won’t be able to read detached logs or transaction log backups.
creating a trigger which will create a new table Emp_audit and add new tuples to it whenever any change is made to table employee
create trigger my_trigger on Employees
AFTER INSERT, UPDATE, DELETE
AS
DECLARE #What varchar(30);
DECLARE #Who varchar(30);
DECLARE #for int;
DECLARE #At time;
DECLARE #COUNTI int;
DECLARE #COUNTD int;
select #COUNTI = COUNT(*) from inserted;
select #COUNTD = COUNT(*) from deleted;
set #Who = SYSTEM_USER;
set #At = CURRENT_TIMESTAMP;
if( #COUNTD = 0 and #COUNTI = 1)
begin
set #What = 'insert';
select #for = EmployeeID from inserted i;
end
else
begin
if( #COUNTD = 1 and #COUNTI = 0)
begin
set #What = 'delete';
select #for = EmployeeID from deleted i;
end
else
begin
set #What = 'update';
select #for = EmployeeID from inserted i;
end
end
INSERT INTO EMP_Audit Values (#What, #Who, #for, #At);
You would be much better off setting up auditing for this need rather than trying to extract this information retrospectively from the transaction log.
If you are on Enterprise Edition you could use the built in SQL Server Audit functionality, otherwise it should be relative straight forward to log the desired information via triggers.
You could create your own transaction logs
Step 1: Create your own table for transaction logs
CREATE TABLE [dbo].[TransactionLogs](
[TransactionLogID] [bigint] IDENTITY(1,1) NOT NULL,
[Query] [nvarchar](max) NOT NULL,
[DateCreated] [datetime] NOT NULL,
CONSTRAINT [PK_TransactionLogs] PRIMARY KEY CLUSTERED
(
[TransactionLogID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Step 2: Create stored procedure that create logs. (Note: Replace YourTablePKColumn with your table primary key column.)
create procedure [dbo].[sp_CreateQueryLogs]
(
#Query nvarchar(max) = null output,
#TableName nvarchar(100),
#YourTablePKColumn nvarchar(30),
#QueryTypeID tinyint --0 insert, 1 update, 2 delete
)
as
begin
declare #object_id bigint, #column_name nvarchar(100), #collation_name nvarchar(50), #column_name_id nvarchar(100) = null, #column_names nvarchar(max) = '', #column_values nvarchar(max) = '', #column_names_create nvarchar(max) = '', #values nvarchar(max) = '', #user_type_id int, #max_length nvarchar(10), #type_name nvarchar(50), #CreateTempTable nvarchar(max) = '', #is_nullable bit, #value nvarchar(max) = ''
create table #tmpValues(ColumnValues nvarchar(max))
insert into #tmpValues(ColumnValues)
exec('select CAST ( ( select * from ' + #TableName + ' where YourTablePKColumn = ' + #YourTablePKColumn + '
FOR XML PATH(''tr''), TYPE
) AS NVARCHAR(MAX) )')
select #values = ColumnValues from #tmpValues
if #QueryTypeID = 0 --insert
set #Query = 'insert into ' + #TableName + '('
else if #QueryTypeID = 1 --update
set #Query = 'update ' + #TableName + ' set '
else if #QueryTypeID = 2 --dalete
set #Query = 'delete ' + #TableName + ' '
select #object_id = object_id from sys.tables where name = #TableName
if not cursor_status('local','columnCursor') <= -1
begin
close columnCursor;
deallocate columnCursor;
end
declare columnCursor cursor local for
select name, user_type_id, convert(nvarchar(10), max_length), is_nullable from sys.columns where object_id = #object_id order by column_id ;
open columnCursor;
fetch next from columnCursor
into #column_name, #user_type_id, #max_length, #is_nullable;
while ##FETCH_STATUS = 0
begin
select #type_name = name, #collation_name = collation_name from sys.types where user_type_id = #user_type_id
if #column_name_id is null
set #column_name_id = #column_name
else
begin
set #column_names += #column_name + ', '
declare #value_keys_start nvarchar(max) = '<' + #column_name + '>', #value_keys_end nvarchar(max) = '</' + #column_name + '>'
if charindex(#value_keys_start,#values,1) = 0
begin
if #QueryTypeID = 0 --insert
set #column_values += 'null,'
else if #QueryTypeID = 1 --update
set #column_values += #column_name + ' = null,'
end
else
begin
if #QueryTypeID = 0 --insert
if #collation_name is null and not (#type_name like '%date%' or #type_name like '%time%')
set #column_values += substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))) + ','
else if #type_name like '%date%' or #type_name like '%time%'
set #column_values += '''' + replace(substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))),'T',' ') + ''','
else
set #column_values += '''' + replace(substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))),'''','''''') + ''','
else if #QueryTypeID = 1 --update
if #collation_name is null and not (#type_name like '%date%' or #type_name like '%time%')
set #column_values += #column_name + '=' + substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))) + ','
else if #type_name like '%date%' or #type_name like '%time%'
set #column_values += #column_name + '=' + '''' + replace(substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))),'T',' ') + ''','
else
set #column_values += #column_name + '=' + '''' + replace(substring(#values, charindex(#value_keys_start,#values,1) + len(#value_keys_start), charindex(#value_keys_end,#values,1) - (charindex(#value_keys_start,#values,1) + len(#value_keys_start))),'''','''''') + ''','
end
end
fetch next from columnCursor
into #column_name, #user_type_id, #max_length, #is_nullable;
end
if not cursor_status('local','columnCursor') <= -1
begin
close columnCursor;
deallocate columnCursor;
end
if #QueryTypeID = 0 --insert
set #Query += substring(#column_names,1,len(#column_names) - 1) + ')
values (' + substring(#column_values,1,len(#column_values) - 1) + ')'
else if #QueryTypeID = 1 --update or delete
set #Query += substring(#column_values,1,len(#column_values) - 1) + ' where YourTablePKColumn = ' + #YourTablePKColumn
else
set #Query += ' where YourTablePKColumn = ' + #YourTablePKColumn
end
Step 3: Created trigger to table you want to have transaction logs
CREATE TRIGGER trg_MyTrigger ON YouTableName
AFTER INSERT, DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
declare #TableName nvarchar(100) = 'YouTableName', #Query nvarchar(max), #QueryTypeID tinyint, #YourTablePKColumn nvarchar(30)
if exists(select * from deleted) and exists(select * from inserted)
begin
set #QueryTypeID = 1
if not cursor_status('local','updatedCursor') <= -1
begin
close updatedCursor;
deallocate updatedCursor;
end
declare updatedCursor cursor local for
select cast(YourTablePKColumn as nvarchar(30)) from inserted;
open updatedCursor;
fetch next from updatedCursor
into #YourTablePKColumn;
while ##FETCH_STATUS = 0
begin
exec dbo.sp_CreateQueryLogs #Query = #Query output, #TableName = #TableName, #YourTablePKColumn = #YourTablePKColumn, #QueryTypeID = #QueryTypeID
insert into TransactionLogs
(Query, DateCreated)
values (#Query,getdate())
fetch next from updatedCursor
into #YourTablePKColumn;
end
if not cursor_status('local','updatedCursor') <= -1
begin
close updatedCursor;
deallocate updatedCursor;
end
end
else if exists(select * from deleted) and not exists(select * from inserted)
begin
set #QueryTypeID = 2
if not cursor_status('local','deletedCursor') <= -1
begin
close deletedCursor;
deallocate deletedCursor;
end
declare deletedCursor cursor local for
select cast(YourTablePKColumn as nvarchar(30)) from deleted;
open deletedCursor;
fetch next from deletedCursor
into #YourTablePKColumn;
while ##FETCH_STATUS = 0
begin
exec dbo.sp_CreateQueryLogs #Query = #Query output, #TableName = #TableName, #YourTablePKColumn = #YourTablePKColumn, #QueryTypeID = #QueryTypeID
insert into TransactionLogs
(Query, DateCreated)
values (#Query,getdate())
fetch next from deletedCursor
into #YourTablePKColumn;
end
if not cursor_status('local','deletedCursor') <= -1
begin
close deletedCursor;
deallocate deletedCursor;
end
end
else
begin
set #QueryTypeID = 0
if not cursor_status('local','insertedCursor') <= -1
begin
close insertedCursor;
deallocate insertedCursor;
end
declare insertedCursor cursor local for
select cast(YourTablePKColumn as nvarchar(30)) from inserted;
open insertedCursor;
fetch next from insertedCursor
into #YourTablePKColumn;
while ##FETCH_STATUS = 0
begin
exec dbo.sp_CreateQueryLogs #Query = #Query output, #TableName = #TableName, #YourTablePKColumn = #YourTablePKColumn, #QueryTypeID = #QueryTypeID
insert into TransactionLogs
(Query, DateCreated)
values (#Query,getdate())
fetch next from insertedCursor
into #YourTablePKColumn;
end
if not cursor_status('local','insertedCursor') <= -1
begin
close insertedCursor;
deallocate insertedCursor;
end
end
END
GO

MsSQL to MySQL function conversion

So I am trying to convert a function I created in MSSQL to MYSQL. The way I have it written in MSSQL is:
ALTER function FormatDate(#date datetime) returns varchar(10)
begin
declare #salida varchar(10)
if (#date != '') and (#date != '01/01/1900')
begin
declare #day varchar(2)
set #day = cast(day(#date) as varchar)
if len(#day) = 1
set #day = '0' + #day
declare #month varchar(2)
set #month = cast(month(#date) as varchar)
if len(#month) = 1
set #month = '0' + #month
select #salida = #month + '/' + #day + '/' + cast(year(#date) as varchar)
end
else
set #salida = null
return #salida
end
I am trying to convert that function into a MYSQL function. I tried this:
Delimiter $$
create function FormatDate(tiempo datetime)
RETURNS varchar(10)
READS SQL DATA
BEGIN
declare salida varchar(10);
if ((tiempo != '') and (tiempo != '01/01/1900')) then
BEGIN
declare dia varchar(2);
set dia = cast(day(tiempo) as varchar);
if len(dia) = 1 then
set dia = '0' + dia;
END IF;
declare mes varchar(2);
set mes = cast(month(tiempo) as varchar);
if len(mes) = 1 then
set mes = '0' + mes;
END IF;
select salida = mes + '/' + dia + '/' + cast(year(tiempo) as varchar);
else
set salida = null;
END; End if;
return (salida);
END $$
Delimiter ;
but I get an error when I try to execute that code.
This is the error I am getting:
Error Code: 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 'varchar);
if len(dia) = 1 then
' at line 14
Can someone please help me convert this MSSQL function into a MYSQL function?
The function to determine a strings lenght in MySQL is called LENGTH(), not len()
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_length
MORE:
I added a working version of your function below. But please note that the built-in function DATE_FORMAT() does exactly what you want:
mysql> SELECT FormatDate( NOW() ), DATE_FORMAT( NOW(), "%m/%d/%Y" );
+---------------------+----------------------------------+
| FormatDate( NOW() ) | DATE_FORMAT( NOW(), "%m/%d/%Y" ) |
+---------------------+----------------------------------+
| 07/15/2011 | 07/15/2011 |
+---------------------+----------------------------------+
You should either use it or replace your function body with a call of that function. Here is, however, a MySQL compatible version of your function:
DELIMITER $$
CREATE FUNCTION `FormatDate`(tiempo datetime) RETURNS varchar(10)
READS SQL DATA
BEGIN
DECLARE salida VARCHAR(10);
DECLARE dia VARCHAR(2);
DECLARE mes VARCHAR(2);
IF ( (tiempo <> '') AND ( tiempo <> '01/01/1900' ) ) THEN
SET dia := CAST( DAY( tiempo ) AS CHAR );
IF LENGTH( dia ) = 1 THEN
SET dia := CONCAT( '0', dia);
END IF;
SET mes := CAST( MONTH( tiempo ) AS CHAR );
IF LENGTH( mes ) = 1 THEN
SET mes := CONCAT( '0', mes );
END IF;
SET salida := CONCAT_WS( '/', mes, dia, CAST( YEAR( tiempo ) AS CHAR ) );
ELSE
SET salida := NULL;
END IF;
RETURN salida;
END $$
DELIMITER ;