Change right to user with escape character - sql-server-2008

I would like to execute the following query :
DENY DELETE ON tableTest to Domain\Username
but it prints
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '\'.
i tried
SELECT #Test = 'Domain\Username'
DENY DELETE ON tableTest to #Test
but it also didn't work.
The Domain\Username is the value i get in sys.database_principals
Any idea?
Thanks

You can use [] to be able to use otherwise invalid characters for identifiers:
DENY DELETE ON tableTest to [Domain\Username]

Related

Having trouble getting MySQL data returned

I have a long Perl script that in other places returns MySQL table data successfully using binds:
$query2 = "SELECT tblExportFiles.CompID, CompEmail, export_id, export_name, query, num_records, sample_rate, record_startnum, Description, Pull_Type, remote_CompID FROM tblExportFiles INNER JOIN tblCustomers USING(CompID) WHERE done=0 ORDER BY export_id ASC ;";
$sqlQuery2 = $dbh->prepare( $query2 );
$sqlQuery2->execute or die "can't execute the query: " . $sqlQuery2->errstr;
$sqlQuery2->bind_columns(
\$CompID, \$CompEmail, \$export_id, \$fileName,
\$queryFile, \$numRecords, \$sampleRate, \$recStartNum,
\$description, \$qType, \$remote_CompID
);
while ( $sqlQuery2->fetch ) { ... }
But when I do the same sort of query here, it fails to return any values but doesn't throw an error:
my $ftpQuerySQL = "SELECT tblResellersData.http_address ,ftp_address, ftp_username, ftp_password, ftp_dir, http_name, tblResellerCustomers.CompEmail FROM tblResellersData, tblResellerCustomers WHERE tblResellerCustomers.User_ID = '$remote_CompID' AND tblResellersData.CompID = '$CompID' ; ";
print "FTP SQL = $ftpQuerySQL\n\n";
$QueryFTP = $dbh->prepare( $ftpQuerySQL );
$QueryFTP->execute() or die "can't execute the query: " . $QueryFTP->errstr;
$QueryFTP->bind_columns(
\$http_address, \$ftp_address, \$ftp_username, \$ftp_password,
\$ftp_dir, \$remote_name, \$CompEmail
);
$QueryFTP->fetch();
It throws warnings
Use of uninitialized value $ftp_address in concatenation (.) or string at ./Cron_file_output.pl line 302.
Use of uninitialized value $ftp_dir in concatenation (.) or string at ./Cron_file_output.pl line 302.
Use of uninitialized value $ftp_username in concatenation (.) or string at ./Cron_file_output.pl line 302.
is a located in
Use of uninitialized value $ftp_dir in scalar chomp at ./Cron_file_output.pl line 303.
Use of uninitialized value $http_address in concatenation (.) or string at ./Cron_file_output.pl line 304.
Use of uninitialized value $ftp_address in concatenation (.) or string at ./Cron_file_output.pl line 304.
Use of uninitialized value $ftp_username in concatenation (.) or string at ./Cron_file_output.pl line 304.
Use of uninitialized value $ftp_password in concatenation (.) or string at ./Cron_file_output.pl line 304.
Use of uninitialized value $ftp_dir in concatenation (.) or string at ./Cron_file_output.pl line 304.
Use of uninitialized value $remote_name in concatenation (.) or string at ./Cron_file_output.pl line 304.
RETURNED VALUES......., , , , , , j#adki87.com
Use of uninitialized value $ftp_address in concatenation (.) or string at ./Cron_file_output.pl line 310.
But when I ran the same SQL under phpMyAdmin, it gave this result:
http_address website's url ftp_address ftp_username ftp_password ftp_dir http_name
http://www.highpeaksbyway.com/ highpeaksbyway.com data#highpeaksbyway.com dataUUU666##) pulls/ TEST ME
What are lines 302, 304, and 310?
It looks like your criteria (the WHERE conditions) are failing, and the statement returns no records
What does $QueryFTP->fetch return? You need to check its status before you use it. That is the major difference between the code that you think "works" and your problem case
You need to check the values of $CompID and $remote_CompID before the execute. You should also use placeholders in the prepare call, and supply the values in execute

How do I fix this SQL syntax error, I cannot find it

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 'INSERT INTO ******_sports_teams (******_team_id,
******_content_type_id, ******' at line 2
That is the error I get to this script;
The stars cover the name of the company I work for that I cannot release, but they are all the same word that I know is spelled correctly.
SELECT * FROM `******_sports_teams` WHERE 1
INSERT INTO ******_sports_teams
(******_team_id, ******_content_type_id, ******_content_type_desc,
******_sport_name, ******_sport_confernece, ******_sport_division,
******_sport_city, ******_sport_team)
VALUES
(1,4,'Sports','NBA','Eastern','Atlantic','Boston','Celtics'),
(2,4,'Sports','NBA','Eastern','Atlantic','Brooklyn','Nets'),
(3,4,'Sports','NBA','Eastern','Atlantic','Newy York','Knicks'),
(4,4,'Sports','NBA','Eastern','Atlantic','Philedelphia','76ers'),
(5,4,'Sports','NBA','Eastern','Atlantic','Toronto','Raptors'),
(6,4,'Sports','NBA','Eastern','Central','Chicago','Bulls'),
(7,4,'Sports','NBA','Eastern','Central','Cleveland','Cavaliers'),
(8,4,'Sports','NBA','Eastern','Central','Detriot','Pistons'),
(9,4,'Sports','NBA','Eastern','Central','Indiana','Pacers'),
(10,4,'Sports','NBA','Eastern','Central','Milwaukee','Bucks'),
(11,4,'Sports','NBA','Eastern','Southeast','Atlanta','Hawks'),
(12,4,'Sports','NBA','Eastern','Southeast','Charlotte','Hornets'),
(13,4,'Sports','NBA','Eastern','Southeast','Miami','Heat'),
(14,4,'Sports','NBA','Eastern','Southeast','Orlando','Magic'),
(15,4,'Sports','NBA','Eastern','Southeast','Washington','Wizards'),
(16,4,'Sports','NBA','Western','Southwest','Dallas','Mavericks'),
(17,4,'Sports','NBA','Western','Southwest','Houston','Rockets'),
(18,4,'Sports','NBA','Western','Southwest','Memphis','Grizzlies'),
(19,4,'Sports','NBA','Western','Southwest','New Orleans','Pelicans'),
(20,4,'Sports','NBA','Western','Southwest','San Antonio','Spurs'),
(21,4,'Sports','NBA','Western','Northwest','Denver','Nuggets'),
(22,4,'Sports','NBA','Western','Northwest','Minnesota','Timber Wolves'),
(23,4,'Sports','NBA','Western','Northwest','Oaklohoma City','Thunder'),
(24,4,'Sports','NBA','Western','Northwest','Portland','Trail Blazers'),
(25,4,'Sports','NBA','Western','Northwest','Utah','Jazz'),
(26,4,'Sports','NBA','Western','Pacific','Golden State','Warriors'),
(27,4,'Sports','NBA','Western','Pacific','Los Ageles','Clippers'),
(28,4,'Sports','NBA','Western','Pacific','Los Ageles','Lakers'),
(29,4,'Sports','NBA','Western','Pacific','Pheonix','Suns'),
(30,4,'Sports','NBA','Western','Pacific','Sacremento','Kings'),
(31,4,'Sports','MLB','American','East','Tampa Bay','Rays'),
(32,4,'Sports','MLB','American','East','New York','Yankees'),
(33,4,'Sports','MLB','American','East','Baltimore','Orioles'),
(34,4,'Sports','MLB','American','East','Toronto','Blue Jays'),
(35,4,'Sports','MLB','American','East','Boston','Red Socks'),
(36,4,'Sports','MLB','American','Central','Kansas City','Royals'),
(37,4,'Sports','MLB','American','Central','Minnesota','Twins'),
(38,4,'Sports','MLB','American','Central','Detriot','Tigers'),
(39,4,'Sports','MLB','American','Central','Cleveland','Indians'),
(40,4,'Sports','MLB','American','Central','Chicago','White Sox'),
(41,4,'Sports','MLB','American','West','Houston','Astros'),
(42,4,'Sports','MLB','American','West','Texas','Rangers'),
(43,4,'Sports','MLB','American','West','Los Ageles','Angels'),
(44,4,'Sports','MLB','American','West','Seattle','Mariners'),
(45,4,'Sports','MLB','American','West','Oakland','Athletics'),
(46,4,'Sports','MLB','National','East','New York','Mets'),
(47,4,'Sports','MLB','National','East','Washington','Nationals').
(48,4,'Sports','MLB','National','East','Atlanta','Braves'),
(49,4,'Sports','MLB','National','East','Philedelphia','Phillies'),
(50,4,'Sports','MLB','National','East','Miami','Marlins'),
(51,4,'Sports','MLB','National','Central','St. Louis','Cardinals'),
(52,4,'Sports','MLB','National','Central','Pittsburg','Pirates'),
(53,4,'Sports','MLB','National','Central','Chicago','Cubs'),
(54,4,'Sports','MLB','National','Central','Cincinnati','Reds'),
(55,4,'Sports','MLB','National','Central','Milwaukee','Brewers'),
(56,4,'Sports','MLB','National','West','Los Ageles','Dodgers'),
(57,4,'Sports','MLB','National','West','San Francisco','Giants'),
(58,4,'Sports','MLB','National','West','Arizona','Diamondbacks'),
(59,4,'Sports','MLB','National','West','San Diego','Padres'),
(60,4,'Sports','MLB','National','West','Colorado','Rockies'),
(61,4,'Sports','NFL','NFC','East','Dallas','Cowboys'),
(62,4,'Sports','NFL','NFC','East','New York','Giants'),
(63,4,'Sports','NFL','NFC','East','Philedelphia','Eagles'),
(64,4,'Sports','NFL','NFC','East','Washington','Redskins'),
(65,4,'Sports','NFL','NFC','West','Arizona','Cardinals'),
(66,4,'Sports','NFL','NFC','West','San Francisco','49ers'),
(67,4,'Sports','NFL','NFC','West','Seattle','Seahawks'),
(68,4,'Sports','NFL','NFC','West','St. Louis','Rams'),
(69,4,'Sports','NFL','NFC','North','Chicago','Bears'),
(70,4,'Sports','NFL','NFC','North' ,'Detriot','Lions'),
(71,4,'Sports','NFL','NFC','North' ,'Green Bay','Packers'),
(72,4,'Sports','NFL','NFC','North' ,'Minnesota','Vikings'),
(73,4,'Sports','NFL','NFC','South','Atlanta','Falcons'),
(74,4,'Sports','NFL','NFC','South', 'Carolina','Panthers'),
(75,4,'Sports','NFL','NFC','South','New Orleans','Saints'),
(76,4,'Sports','NFL','NFC','South','Tampa Bay','Buccaneers'),
(77,4,'Sports','NFL','AFC','East','Buffalo','Bills'),
(78,4,'Sports','NFL','AFC','East','Miami','Dolphins'),
(79,4,'Sports','NFL','AFC','East','New England','Patriots'),
(80,4,'Sports','NFL','AFC','East','New York','Jets'),
(81,4,'Sports','NFL','AFC','West','Denver','Broncos'),
(82,4,'Sports','NFL','AFC','West','Kansas City','Chiefs'),
(83,4,'Sports','NFL','AFC','West','Oakland','Raiders'),
(84,4,'Sports','NFL','AFC','West','San Diego','Chargers'),
(85,4,'Sports','NFL','AFC','North' ,'Baltimore','Ravens'),
(86,4,'Sports','NFL','AFC','North' ,'Cleveland','Browns'),
(87,4,'Sports','NFL','AFC','North','Pittsburg','Steelers'),
(88,4,'Sports','NFL','AFC','North' ,'Cincinnati','Bengals'),
(89,4,'Sports','NFL','AFC','South','Houston','Texans'),
(90,4,'Sports','NFL','AFC','South','Indianapolis','Colts'),
(91,4,'Sports','NFL','AFC','South','Jacksonville','Jaguars'),
(92,4,'Sports','NFL','AFC','South','Tennessee','Titans'),
(93,4,'Sports','NHL','NHL','Central','Chicago','Blackhawks'),
(94,4,'Sports','NHL','NHL','Central','Colorado','Avalanche'),
(95,4,'Sports','NHL','NHL','Central','Dallas','Stars'),
(96,4,'Sports','NHL','NHL','Central','Minnesota','Wild'),
(97,4,'Sports','NHL','NHL','Central','St. Louis','Blues'),
(98,4,'Sports','NHL','NHL','Central','Winnipeg','Jets'),
(99,4,'Sports','NHL','NHL','Central','Nashville','Predators'),
(100,4,'Sports','NHL','NHL','Pacific','Anaheim' ,'Ducks'),
(101,4,'Sports','NHL','NHL','Pacific','Arizona','Coyotes'),
(102,4,'Sports','NHL','NHL','Pacific','Clagary','Flames'),
(103,4,'Sports','NHL','NHL','Pacific','Edmonton','Oilers'),
(104,4,'Sports','NHL','NHL','Pacific','Los Ageles','Kings'),
(105,4,'Sports','NHL','NHL','Pacific','San Jose','Sharks'),
(106,4,'Sports','NHL','NHL','Pacific','Vancouver','Canucks'),
(107,4,'Sports','NHL','NHL','Atlantic','Boston','Bruins'),
(108,4,'Sports','NHL','NHL','Atlantic','Buffalo','Sabres'),
(109,4,'Sports','NHL','NHL','Atlantic','Detriot','Red Wings'),
(110,4,'Sports','NHL','NHL','Atlantic','Florida','Panthers'),
(111,4,'Sports','NHL','NHL','Atlantic','Montreal','Canadiens'),
(112,4,'Sports','NHL','NHL','Atlantic','Ottawa','Senators'),
(113,4,'Sports','NHL','NHL','Atlantic','Tampa Bay','Lightning'),
(114,4,'Sports','NHL','NHL','Atlantic','Toronto','Maple Leafs'),
(115,4,'Sports','NHL','NHL','Metropolitan','Corolina','Hurricanes'),
(116,4,'Sports','NHL','NHL','Metropolitan','Columbus','Blue Jackets'),
(117,4,'Sports','NHL','NHL','Metropolitan','New Jersey','Devils'),
(118,4,'Sports','NHL','NHL','Metropolitan','New York','Islanders'),
(119,4,'Sports','NHL','NHL','Metropolitan','New York','Rangers'),
(120,4,'Sports','NHL','NHL','Metropolitan','Philedelphia','Flyers'),
(121,4,'Sports','NHL','NHL','Metropolitan','Pittsburg','Penguins'),
(122,4,'Sports','NHL','NHL','Metropolitan','Washington','Capitals')
you have a dot (.) instead of a comma (,) at the end of the line with ID 47:
(47,4,'Sports','MLB','National','East','Washington','Nationals').
Change it to
(47,4,'Sports','MLB','National','East','Washington','Nationals'),
You have a period at the end of this line:
(47,4,'Sports','MLB','National','East','Washington','Nationals').
It should be a comma
Your error message show us, that two queries does not separated with ;
Just remove Select query, or add ; after it.
Then, double check syntax, or read next error message. You have points instead of comma somewhere.
Your first query retrieve all data from the table. It's:
SELECT * FROM `******_sports_teams` WHERE 1
It should terminated with semicolon before you call next (INSERT) query

SQL Bound variables error

I'm getting the error in my code. I'm sure this means there is something wrong with the column=:variable section of my code, but I've gone over it and can't see what's wrong.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens' in F:\...
Here is the code (long first line):
$stmt = $dbh->prepare("UPDATE item_list SET quantity=:quantity, new_price=:newprice, image_link=:image_link, market_hash_name=:markethashname, last_update='1000-01-01 00:00:00', is_tradable='TEST', old_price=:oldprice WHERE item_name=:itemname AND (image_link IS NULL OR quantity IS NULL OR new_price IS NULL OR market_hash_name IS NULL OR last_update IS NULL OR is_tradable IS NULL OR old_price IS NULL)");
$sql->bindParam(':quantity', $json2->quantity);
$sql->bindParam(':newprice', $json2->lowest_price);
$sql->bindParam('image_link', $image_link_full);
$sql->bindParam(':markethashname', urlencode($mydata->market_hash_name));
$sql->bindParam(':oldprice', $json2->lowest_price);
$stmt->bindValue(':itemname', $mydata->market_name);
$stmt->execute();
EDIT: image_link needed to be :image_link, still getting the same error
EDIT 2: Had a lot wrong, was using two names $sql and $stmt, using bindValue instead of bindParam
you are missing a colon ':' before the 'image_link' in the bindParam statement.

Rails - Strange characters pass through validation and break query

I copy-pasted a string into a form field and a strange character broke my MySql query.
I could force the error on the console this way (the weird character is in the middle of the two words "Invalid" and "Character", you can also copy-paste it):
> dog.name = "Invalid ​Character"
> dog.save # -> false
Which returns the following error:
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect string value: '\xE2\x80\x8BCha...' for column 'name' at row 1: UPDATE `dogs` SET `name` = 'Invalid ​Character' WHERE `dogs`.`id` = 2227
It replaced the character by '\xE2\x80\x8B' as the error said.
Is there any validation that I could use to remove these kind of weird characters?
Obs: I also saw that
> "Invalid ​Character".unpack('U*')
Returns
[73, 110, 118, 97, 108, 105, 100, 32, 8203, 67, 104, 97, 114, 97, 99, 116, 101, 114]
The weird character must be the 8230 one.
Obs2: In my application.rb, I have: config.encoding = "utf-8"
EDIT
On my console, I got:
> ActiveRecord::Base.connection.charset # -> "utf8"
> ActiveRecord::Base.connection.collation # -> "utf8_unicode_ci"
I also ran (on the rails db mySql console):
> SELECT table_collation FROM INFORMATION_SCHEMA.TABLES where table_name = 'dogs';
and got "utf8_unicode_ci"
EDIT2
If I change the table's character set to utf8mb4 I don't get the error. But still, I have to filter those characters.
On the rails db MySql console, I used:
SHOW CREATE TABLE dogs;
To find out that the charset for the table was latin1.
I just added a migration with this query:
ALTER TABLE dogs CONVERT TO CHARACTER SET utf8mb4;
And it started to work fine.

Failed to insert value into tables in Ruby Mysql

I am trying to use Ruby to insert values into MySQL on localhost. The value i want to insert is the result from Twitter search. My program can successfully write the result to a file, so now i want to insert the result to MySQL. Here is part of my code:
results = #search.perform("yahoo", 100)
client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123", :database => "db1")
results.map do |status|
insert = client.query ("INSERT INTO table1 (fromuser, tweet) VALUES (#{status.from_user},#{status.text})")
end
The error is "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 " tweet " at line 1 (Mysql2::Error).
What caused the error?
Another problem i have found is that when i used following code to insert value to MySQL ,i got another error: "Unknown column 'a' in 'field list' (Mysql::ServerError::BadFieldError)"
require 'mysql'
require 'rubygems'
sql = Mysql.real_connect("localhost", "root", "123", "db1")
st1 = "a"
st2 = "b"
user_string = "(#{st1},#{st2})"
query="INSERT INTO table1 (fromuser, tweet) VALUES" + user_string
sql.query(query)
I want to insert "a" and "b" into table.
How to solve this?
Thanks in advance,
Like Andrew said, you definitely want to escape your data.
I think you also need to quote the values:
insert = client.query("INSERT INTO tweets (from_user, tweet_text)
VALUES ('#{client.escape(status.from_user)}',
'#{client.escape(status.text)}')")
You need to use CREATE TABLE to create a table in your database to insert the data into. At the moment you are saying you want to insert the data into the database name ("db1") itself.
Also, you must escape your data first:
insert = client.query("INSERT INTO tweets (from_user, tweet_text)
VALUES (#{client.escape(status.from_user)},
#{client.escape(status.text)})")