How to add id automatically in SAS - mysql

I have the following SAS code:
PROC SQL;
CREATE TABLE HUB_Addresses (
AddressID INT AUTO_INCREMENT,
LOAD_DATE NUM FORMAT=DATETIME22. NOT NULL,
RECORD_SOURCE VARCHAR(255) NOT NULL
);
RUN;
quit;
But AUTO_INCREMENT is not working. How can I fix it

You Can Add other columns and run.
CREATE TABLE `HUB_Addresses` (
`AddressID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`AddressID`))
ENGINE = MyISAM;

RDBMS features found in other systems, such as Oracle, SQL Server or Netezza:
auto_increment not in SAS SQL
triggers not in SAS SQL
default values not in SAS SQL
sequences not in SAS SQL
If your data design depends on auto_increment you will have to use a third party data base and the appropriate SAS/ACCESS engine to interact with it.
Proc SQL lacks the listed features because the default Base SAS library engine (V9) does not support such features.
If you already have a MySQL connection available and a SAS/ACCESS engine to interoperate:
SAS/ACCESS to MySQL or
SAS/ACCESS to ODBC with MySQL ODBC drivers installed
You can use pass-through syntax to submit MySQL statements directly to the data base server.
libname REMOTE MYSQL … connection options …;
PROC SQL;
connect using REMOTE;
execute (
/* MySQL statement */
CREATE TABLE HUB_Addresses (
AddressID INT AUTO_INCREMENT,
LOAD_DATE DATE NOT NULL,
RECORD_SOURCE VARCHAR(255) NOT NULL
)
by REMOTE;
* insertion occurs through SAS/ACCESS engine via REMOTE libref;
insert into REMOTE.HubAddresses
values (., '01FEB2010'D, 'Manual insert')
;

Related

how to transfer from MySql to Management Studio

I have database which works on MySql. And i need to copy it to another computer where there is only management studio. i create .sql file from MySql and tried to run it in studio but always have syntax errors.
How to do it correctly?
CREATE TABLE branches (
idbranches int(11) NOT NULL AUTO_INCREMENT,
address varchar(200) NOT NULL,
e.g. incorrect syntax for "AUTO_INCREMENT"
The SQL Server syntax is:
CREATE TABLE branches (
idbranches int identity(1, 1) not null,
address varchar(200) NOT NULL
);

Importing database into WAMP Server

I have a project sent to me by a friend and i am having serious issues importing the database into my WAMP Server. I end up getting Mysql error
Error
SQL query:
--
-- Database: `drivers_endorsements`
--
-- --------------------------------------------------------
--
-- Table structure for table `admin`
--
CREATE TABLE IF NOT EXISTS `admin` (
`admin_id` int(11) NOT NULL,
`username` varchar(30) NOT NULL,
`password` varchar(12) NOT NULL,
`name` varchar(40) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3
MySQL said: Documentation
#1046 - No database selected
Firstly you have to create database manually or select the existing one in phpmyadmin (as mentioned WAMP server is used ) and then import the .sql file in it, and the database name should be same as that of used in application else will not work with desired application to which the database is linked.
It's not really a big deal! The message itself is self-explanatory. You need to select an existing database first & then try your import.
Or you could possibly add the following at the very top of your DB script that you trying to import -
CREATE DATABASE IF NOT EXISTS drivers_endorsements;
USE drivers_endorsements;

Why won't my SQL passwordhashing-procedure run?

I am trying to create a SQL procedure that hashes password inputs. This code won't run and I am not getting any useful response errors.
The first part creates the table, the second creates the procedure. When I call on my procedure in the third part it send the values into the procedure. There the password is supposed to be hashed using SHA2_512 and inserted into the table we made eralier.
I used online research to make this code, the parts I don't get is:
The N before my values
The SetNoCount
The #responsemessage
-- makes Admin table
CREATE TABLE IF NOT EXISTS `AdminUser` (
`AdminID` smallint(6) NOT NULL AUTO_INCREMENT,
`Username` char(15) NOT NULL,
`PasswordHash` BINARY(64) NOT NULL,
`Fornavn` char(30) NOT NULL,
`Etternavn` char(40) NOT NULL,
`Email` char(40) NOT NULL,
PRIMARY KEY (`AdminID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Makes hashing procedure
CREATE OR REPLACE PROCEDURE vm_ski.addAdmin
#pUsername NVARCHAR(50),
#pPassword NVARCHAR(50),
#pFornavn NVARCHAR(30),
#pEtternavn NVARCHAR(40),
#pEmail NVARCHAR(40),
#responseMessage NVARCHAR(250)='' OUTPUT
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
INSERT INTO vm_ski.AdminUser (Username, PasswordHash, Fornavn, Etternavn, Email)
VALUES(#pUsername, HASHBYTES('SHA2_512', #pPassword), #pFornavn, #pEtternavn, #pEmail)
SET #responseMessage='Success'
END TRY
BEGIN CATCH
SET #responseMessage=ERROR_MESSAGE()
END CATCH
END;
-- Admin example
DECLARE #responseMessage NVARCHAR(250)
EXECUTE vm_ski.addAdmin
#pUsername = N'sondre',
#pPassword = N'example'
#pFornavn = N'Sondre'
#pEtternavn = N'Morgendal'
#pEmail = N'sondre.example#gmail.com'
;
This is not a direct answer to the question; this is a security note on the methodology of the question
Do NOT hash passwords in MySQL. The data given to MySQL is plaintext, and easily intercepted by MySQL processing logs as well as possibly numerous other places before being dumped in the database (such as if message packets sent to the database are non-localhost and are non-TLS). ( Why? )
When hashing passwords you want to be doing so as early in the process as possible. This typically means using PHP password_hash and simply dumping only the hashed data in the MySQL.
If you do not use PHP to interact with your SQL then you can use other server methods such as Argon2 or Libsodium.
Also as a side point you should be using the mb4 UTF-8 charset and collations - principly utf8mb4_general_ci ( Why? )

Converting SQL Server code to MySQL

I am trying to concatenate two integers as the default value in a third field. My create table in SQL Server works fine:
CREATE TABLE MEI_Tbl
(
MEI_ID int PRIMARY KEY IDENTITY (1,1),
SRC tinyint NOT NULL DEFAULT '2',
HEI_ID AS (Cast (SRC as varchar)+ Cast (MEI_ID as varchar))
);
but when I try to create it in MySQL, I cannot find the equivalent for the concatenation of the two integers (Line 5 HEI_ID...).
**
I am aware of changing IDENTITY (1,1) to AUTO_INCREMENT for MySQL.
**
I have also tried several concat methods, but to no avail.
MySQL seems happier if I define the datatype for HEI_ID, and I have done so as varchar and int but again no success.
I have spent too much time reading about tool kits to convert SQL Server to MySQL. I just want to create the table in MySQL.
Any input would be appreciated.
MySQL does not support computed columns. Instead, you can use a view:
CREATE TABLE MEI_Tbl (
MEI_ID int PRIMARY KEY AUTO_INCREMENT,
SRC tinyint NOT NULL DEFAULT 2
);
CREATE VIEW v_MEI_Tbl as
SELECT MEI_ID, SRC,
CONCAT(src, mei_d) as HEI_ID
FROM MEI_Tbl
);
Then query from the view.

SQL server openquery - "Key column information is insufficient or incorrect. Too many rows were affected by update."

I'm trying to run a delete query from Micrsoft SQL server to clear out a table in a remote mySQL table.
Query is:
delete from OPENQUERY(WEB_DB,'select id,university_name,college_name from table_name');
This is returning the error:
OLE DB provider "MSDASQL" for linked server "WEB_DB" returned
message "Key column information is insufficient or incorrect. Too many
rows were affected by update.". Msg 7345, Level 16, State 1, Line 1
The OLE DB provider "MSDASQL" for linked server "WEB_DB" could not
delete from table "select id,university_name,college_name from
table_name". Updating did not meet the schema
requirements.
The SQL used to create the table is as follows:
CREATE TABLE IF NOT EXISTS `table_name` (
`pk` int(11) AUTO_INCREMENT,
`studentid` int(11) default NULL,
`university_name` varchar(255) default NULL,
`college_name` varchar(255) default NULL,
CONSTRAINT PRIMARY KEY(`pk`)
);
Interestingly it does appear to do delete some portion of the remote database records, there should be ~900, running the delete command for the first time reduces this to ~700 so it seems like the limit is about 200 rows?!
Any help much appreciated.
Thanks
Jona
If you are trying to delete all the rows in the remote table, why not:
DELETE WEB_DB...table_name;
Or if this is something you're doing often, why not put a stored procedure on the other end to call?
You might also try:
EXEC ('delete table_name') AT WEB_DB;
To clear out the table in mysql using openquery, you should try this:
EXEC('TRUNCATE TABLE table_name') AT WEB_DB
The syntax below resolved it for me:
EXEC ('delete table_name where a in (1,2,3)') AT linked_server