set n=901234567;
set p=0;
insert into try values (
concat(fname,i),
concat(sname,i),
concat(age,i),
concat(email,i,id),
concat(n,p)
);
set n=n+1;
set i=i+1;
concat is working for first four columns.but its not working for concating n and p.here am incrementing the value of n .And for every incrementation the value in p i.e 0 sholud be added at the end of the value of n.
What are n and p? System variables? Most probably not. There is no need to search them into the list of MySQL server system variables, their names are too short, there is no chance to find them there.
Oh, wait! They are user-defined variables. Or, at least, this is what you want them to be. Except that the user-defined MySQL variables are always prefixed with #.
Back to the code, n and p are not user-defined variables and the user is not allowed to create system variables. MySQL returns the error Unknown system variable 'n' on the first SET query. Case closed.
Let's try to make it work:
set #n=901234567;
set #p=0;
insert into try values (
concat(fname,#i),
concat(sname,#i),
concat(age,#i),
concat(email,#i,id),
concat(#n,#p)
);
set #n=#n+1;
set #i=#i+1;
If the other names present in the queries (fname, sname, age, email, id) are also variables then put # in front of them.
Related
i am using this piece of code to replace characters in one column of my database.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
But now I have a list with almost 500 characters to replace.
Just writing the whole sequence of lines in one single query will not work.
UPDATE items
SET items = REPLACE(items, 'ḇ','ḇ')
SET items = REPLACE(items, 'Ǡx;','x')
SET items = REPLACE(items, 'Ǡy;','y')
ETC.
Or I do not know how to write it.
Can anyone help me please?
Create a table that has the search string and the replace string as columns. Add all 500 rows of what needs to be replaced. Then write a stored procedure that will lookup the replace value from the lookup table and replace with the value. The lookup table can easily be loaded into MySql from an Excel or csv file.
Here's the pseudo code to show the looping and lookup. I know it won't compile, I'm a bit rusty on MySql syntax. I usually work in Oracle so the pseudocode syntax is more Oracle-esque.
DECLARE
v_old_string varchar;
v_new_string varchar;
BEGIN
FOR v IN (SELECT * FROM items) LOOP
SELECT old_string, new_string
INTO v_old_string, v_new_string
FROM my_lookup_table
WHERE old_string = v.thestringcolumn;
UPDATE items
SET itemcolumn = REPLACE(itemcolumn, v_old_string, v_new_string)
WHERE itemcolumn = v_old_string;
END LOOP;
END;
I have a column within my database that holds text similar to this
CNEWS # Trinidad : "By Any Means Necessary" Watson Duke Swims And Sails To Toco http://somewebsitehere.com
What can I do to remove the entire http address from the column? Please note that some links may be broken so it may have http:// somewebsitehere.com
I was thinking of using a substring index but not sure that would work.
You could use whichever your favorite programming language is to iterate through the rows in the table, pluck out that column, apply a regular expression replacement rule to it, then update the row in the table with the new value.
Here is some pseudo-code:
theRows = SELECT * FROM TheTable WHERE 1;
foreach row in theRows
BEGIN
oldColumnValue = row[theColumnName]
// Removes any link appearing at the end of the column
newColumnValue = oldColumnValue.replace(/http:\/\/[^\s]*$/, '')
UPDATE TheTable SET theColumnName = newColumnValue WHERE id = row[id]
END
For something as small and specific as this, you could use perl with the DBI library to connect to mySQL. Here's a useful resource on regular expressions if you want to go more into it: http://www.regular-expressions.info/perl.html
In some cases, we are using IN clause in our filters of SSRS reports. A lots of them are causing issues with the performance by using hundreds of items inside of IN clauses.
such as:
WHERE TableA.School IN (#School)
sometimes, the multi-value parameters are really tricky to handle, you might need to do =Join(Mypara.Value,",") in the RDL and write a SQL function to convert them into a set of SQL data to be able to feed the SQL SP. (especially some older version of SSRS).
FYI: function to use to break a comma deliminator string into record set:
CREATE function [dbo].[fnSpark_BreakUpList] (
#List VARCHAR(MAX)
)
RETURNS #csvlist TABLE (Item VARCHAR(MAX))
AS
BEGIN
DECLARE #Item VARCHAR(MAX)
-- Loop through each item in the comma delimited list
WHILE (LEN(#List) > 0)
BEGIN
IF CHARINDEX(',',#list) > 0
BEGIN
SET #Item = SUBSTRING(#List,1,(CHARINDEX(',', #List)-1))
SET #List = SUBSTRING(#List,(CHARINDEX(',', #List) + DATALENGTH(',')),DATALENGTH(#List))
END
ELSE
BEGIN
SET #Item = #List
SET #List = NULL
END
-- Insert each item into the csvlist table
INSERT into #csvlist (Item) VALUES (#Item)
END
RETURN
END
GO
I will post answers shortly to show how to increase the performance by using CHARINDEX. (So you dont have to anything like above....)
If you are not actually want to retrieve the item from the LONG delimited string, but to only filtering it, CHARINDEX is a better way to go for.
Instead of using IN Clause, you could just use:
WHERE CHARINDEX(','+TableA.School+',',','+#School+',') > 0
NOTE:
1. I pad an extra comma at the end of target string 'TableA.School' to avoid the satuation that if a big string contains a sub string same as the filtering item.
(Such as we have a school called 'AB' and another 'ABC' that we dont want the 'ABC' to be picked up when we are targeting for 'AB'.... )
I pad an extra comma at the end of resources string '#School' to ensure that the single item / the last item (they will end without a comma) to be picked up when we are targeting them.
I pad an extra comma at the begining of target string 'TableA.School' to avoid the satuation that if a big string contains a sub string same as the filtering item.
(Such as we have a school called 'AB' and another 'CAB' that we dont want the 'CAB' to be picked up when we are targeting for 'AB'.... )
Example:
I am using:
WHERE
CHARINDEX(','+CAST(DENTIST4.wStudentYear AS VARCHAR(10))+',',','+#StudentYear+',') > 0
TO replace:
WHERE
DENTIST4.wStudentYear IN (#StudentYear)
for one of the report i was doing, which makes 4000+ pages rendering improved from about 10 mins into under a min for a large database (11 G).
IMPORTANT NOTES:
Please make sure the filter report parameters you are passing into the dataset uses JOIN clause.
=Join(Parameters!MyParameter.Value,",")
Hope this helps....
IMPORTANT: This approach ONLY improves performance if the filter has large set of items, for any filters with small set of items IN clause will do better jobs.
Im sorry for the terrible title, I can't think of a better way to state it...
Essentially, i have a NVARCHAR field that is filled with various production comments that look like this:
(1/22/2015 (blujo) - WO113315 In Process)
I want to pull just the 'WO113315' part out of that field. These are work order numbers, and will obviously be different in each row of the table. So for any field that contains WO% somewhere in it, i want to pull that WO% plus the next 6 characters after it. I don't think trim will work b/c the WO% number could be anywhere in the field. But it will always start with WO%.
You can use a combination of SUBSTRING and CHARINDEX.
DECLARE #VAL AS NVARCHAR(100) = '1/22/2015 (blujo) - WO113315 In Process'
SELECT SUBSTRING(#VAL, CHARINDEX('WO', #VAL), 8)
If you know that the work order number will always show up after the dash, you can also use the following to ensure you are starting the search at this point.
DECLARE #VAL AS NVARCHAR(100) = '1/22/2015 (WOblujo) - WWO113315 In Process'
SELECT SUBSTRING(#VAL, CHARINDEX('WO', #VAL, CHARINDEX('-',#VAL)), 8)
EDIT: If you're not guaranteed of a WO code, then the following case statement can pick it up. At this point, I would suggest saving these results onto a new field on the table (or maybe a persisted computed column) so that your server doesn't need to go through this each time.
DECLARE #VAL AS NVARCHAR(100) = '1/22/2015 (blujo) - RW13315 In Process'
SELECT CASE WHEN CHARINDEX('WO', #VAL, CHARINDEX('-',#VAL)) > 0 THEN
SUBSTRING(#VAL, CHARINDEX('WO', #VAL, CHARINDEX('-',#VAL)), 8)
WHEN CHARINDEX('RW', #VAL, CHARINDEX('-',#VAL)) > 0 THEN
SUBSTRING(#VAL, CHARINDEX('RW', #VAL, CHARINDEX('-',#VAL)), 8)
ELSE NULL END
I want to update a mysql row, but I do not want to specify all the column names.
The table has 9 rows and I always want to update the last 7 rows in the right order.
These are the Fields
id
projectid
fangate
home
thanks
overview
winner
modules.wallPost
modules.overviewParticipant
Is there any way I can update the last few records without specifying their names?
With an INSERT statement this can be done pretty easily by doing this:
INSERT INTO `settings`
VALUES (NULL, ...field values...)
So I was hoping I could do something like this:
UPDATE `settings`
VALUES (NULL, ...field values...)
WHERE ...statement...
But unfortunately that doesn't work.
If the two first columns make up the primary key (or a unique index) you could use replace
So basically instead of writing
UPDATE settings
SET fangate = $fangate,
home = $home,
thanks = $thanks
overview = $overview,
winner = $winner,
modules.wallPost = $modules.wallPost,
modules.overviewParticipant = $modules.overviewParticipant
WHERE id = $id AND procjectId = $projectId
You will write
REPLACE INTO settings
VALUES ($id,
$projectId,
$fangate,
$home,
$thanks
$overview,
$winner,
$modules.wallPost,
$modules.overviewParticipant)
Of course this only works if the row already exist, otherwise it will be created. Also, it will cause a DELETE and an INSERT behind the scene, if that matters.
You can't. You always have to specify the column names, because UPDATE doesn't edit a whole row, it edits specified columns.
Here's a link with the UPDATE syntax:
http://dev.mysql.com/doc/refman/5.0/en/update.html
No, it works on the INSERT because even if you didn't specify the column name but you have supplied all values in the VALUE clause. Now, in UPDATE, you need to specify which column name will the value be associated.
UPDATE syntax requires the column names that will be modified.
Are you always updating the same table and columns?
In that case one way would be to define a stored procedure in your schema.
That way you could just do:
CALL update_settings(id, projectid, values_of_last_7 ..);
Although you would have to create the procedure, check the Mysql web pages for how to do this, eg:
http://docs.oracle.com/cd/E17952_01/refman-5.0-en/create-procedure.html
I'm afraid you can't afford not specifying the column names.
You can refer to the update documentation here.