Putting together a tricky SQL query - sql-server-2008

I am working on a SQL 2008 machine and cannot seem to get the query to work.
My SQL query is :
select q.Document DOC from references q, equiprates e where e.MachineID=q.UnitID'
The rows retruned by q.Document is:
5570_RESTAURANT.pdf
5650_RESTAURANT.pdf
5110_RESTAURANT.pdf
However, I need the table rows to be as follows:
Restaurant Document
5570_RESTAURANT.pdf
5570_RESTAURANT.pdf
5570_RESTAURANT.pdf
So I am trying to format my selecct string as follows:
Select #sSQL = 'select q.Document DOC, ''''+q.Document+'''' ''Restaurant Document'',
from references q, equiprates e
where e.MachineID=q.UnitID'
My error message is:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "q.Document" could not be bound.
Any ideas how to resolve this?
I tried google, but no luck.

Your single quotes are just wrong (I also recommend shifting to more modern INNER JOIN syntax). But why can't the application simply add the HTML around the DOC column? Seems wasteful (never mind more complex, obviously) to add all that HTML at the server, and send all those bytes over the wire.
DECLARE #sSQL NVARCHAR(MAX);
SET #sSQL = N'SELECT
DOC = q.Document,
[Restaurant Document] = ''<a href="Javascript:ViewFile(''''''
+ q.Document + '''''');" class="Link">''
+ q.Document + ''</a>''
FROM references AS q
INNER JOIN equiprates AS e
ON q.UnitID = e.MachineID';
PRINT #sSQL;

try just
select
'' + q.Document + ''
from
references q, equiprates e
where
e.MachineID=q.UnitID
but remember, it's very bad programming style. it's better when the data model and data view are separated from each other

Related

Replace substring from an table with an stringified value of another table

So I have 2 tables and one contains an string with an ID I want to replace with an string by another table.
I came up with this SQL, which should work, but it seems like an LEFT JOIN isn't allowed in this case.
UPDATE sales_channel_api_context AS api
SET api.payload = REPLACE(
api.payload,
SUBSTRING(
api.payload,
LOCATE('paymentMethodId":"', api.payload)+18,
32
),
LOWER(HEX(c.default_payment_method_id))
)
LEFT JOIN customer AS c
ON c.id = api.customer_id
WHERE api.payload LIKE '%paymentMethodId%' AND api.customer_id IS NOT NULL;
Does anyone know an SQL Query that does exactly this, without creating another table?
An temp table can be used but an new permanent table is no solution.
ChatGPT gave me a working solution and it is as follow:
UPDATE sales_channel_api_context
JOIN customer c ON c.id = sales_channel_api_context.customer_id
SET payload =
CASE
WHEN payload LIKE '%paymentMethodId%' THEN
REPLACE(
payload,
SUBSTRING(
payload,
LOCATE('paymentMethodId":"', payload) + 18,
32
),
LOWER(HEX(c.default_payment_method_id))
)
ELSE payload
END
WHERE sales_channel_api_context.customer_id IS NOT NULL;

MySQL Update based on a query in another table

I want to update a table with a query from another table.
I want to get mixed informations from a combination of two tables.
And then update one of the two tables with them.
Here is what I did :
UPDATE commande as C,
(
SELECT CONCAT (input_hauteur_sous_collecteur, ' x ', input_largeur_hors_tout, ' x ', input_epaisseur, ' - ', input_pas_ailettes)
AS 'ligne_sage'
FROM commande as C, faisceaux_ta as F
WHERE C.commande_type_faisceaux = 'TA'
AND C.commande_id_faisceaux = F.id
) AS src
SET
C.ligne_sage = src.ligne_sage
WHERE
C.commande_type_faisceaux = "TA"
/* And I got MySQL running the command and never ending without error notification... */
EDIT : Actually it finally works in more than 5 minutes, the problem is that I have the same values (first line of the SELECT result table) in each lines...
What shall I do to make it work ?
(the SELECT CONCAT subquery is properly working)
You got this because you didn't filter results between tables. You need to add and filter in the where clause. Something like (see last line). I don't have your tables defs :-(
UPDATE commande as C,
(
SELECT CONCAT (input_hauteur_sous_collecteur, ' x ', input_largeur_hors_tout, ' x ', input_epaisseur, ' - ', input_pas_ailettes)
AS 'ligne_sage'
FROM commande as C, faisceaux_ta as F
WHERE C.commande_type_faisceaux = 'TA'
AND C.commande_id_faisceaux = F.id
) AS src
SET
C.ligne_sage = src.ligne_sage
WHERE
C.commande_type_faisceaux = "TA"
AND c.commandId = src.commandId

Getting error using EXECUTE('UPDATE..') on a MySQL server from SQL Server

I intend to do:
EXECUTE('UPDATE tableA SET campaignkey = ''20170101'' where storekey = 16
and campaignkey LIKE ''%,%''') at MYLINKEDSERVER
but I get the error that:
You have an error in your SQL syntax; [...] for the right syntax to use near 'where storekey = 16 and campaignkey LIKE '%,%''
Does anyone have any idea what is wrong? To me it seems like I might have one ' too many on my LIKE statement, but I have Always used '' to indicate a non-numeric value. I don't want to fiddle with this to prevent updating far too many values on this server.
campaignkey is non-numeric (I Believe varchar) and storekey is integer.
Edit
I must not use OPENQUERY() because it is not set up correctly, and this is a urgent update.
Edit 2
Seems like it is because of apostrophes 's in the EXECUTE statement.
When I conduct:
select * from openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
it works, but when using:
EXECUTE('Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''') at linkedserver
I get the error that I need to check the manual by the where clause. From googling it appears however that the correct syntax in fact is:
EXECUTE('UPDATE TableX SET StringVar = ''stringValue'' WHERE intVar = 3
AND stringVar = ''xxxxx''') AT LinkedServer
I don't know why this won't work for me.. I have tried many combinations of '', '" etc.
What about this one?
update openquery(linkedserver,'Select * from tableA where storekey = 16
and campaignkey = ''20170826,151''')
set campaignkey = '20170101'

Update 500+ field records to include an increment value + attribute value

Im looking to update 500+ records in my mysql database so that the fields will be a value combination of an $incremental_value+db_user_first_name+#some_static_text. An example of the wished outcome:
1_firstname#staticstring.com, 2_george#staticstring.com, 3_johnny#staticstring.com etc.
I've been playing around with some approach as the following, but that naturally doesn't work (modified for hopefully better clarification).
UPDATE user
SET email = (($incremental_value+1)+(user.first_name))"#staticstring.com"
WHERE email = "empty#empty.com"
The correct syntax for string concatenation in MySQL is the concat() function:
UPDATE user cross join
(select #i = VALUETOSTART) var
SET email = concat(#i := #i + 1, '_', user.first_name, '#staticstring.com')
WHERE email = 'empty#empty.com';

Inserting images from file path -- Not getting value on the select statement

DECLARE #imgString varchar(800)
DECLARE #insertString varchar(3000)
DECLARE #imgNumber int
Declare #imgName varchar(100)
SET #imgNumber = 1
WHILE #imgNumber<> 101
BEGIN
SET #imgName = 'SELECT (items) FROM dbo.building_piclink'
SET #imgString = 'C:\Documents and Settings\Administrator\Desktop\photos\' + #imgName
SET #insertString = 'INSERT INTO dbo.building__ATTACH (DATA)
SELECT * FROM OPENROWSET(BULK N''' + #imgString + ''', SINGLE_BLOB) as tempImg'
SET #imgNumber = #imgNumber + 1
END
GO
I am having problems with the #imgName. I can't figure out how to get the value from the select statement not the (items) like below:
C:\Documents and Settings\Administrator\Desktop\photos\SELECT (items) FROM dbo.building_piclink
Thank you!
Your code has several problems:
1) You're selecting a file name from the view - but what if that view contains more than one entry?? Which filename are you selecting?? Your current code first of all doesn't work at all the way it is, and even if it were working - you're still potentially selecting hundreds of filenames into a single variable - which of course won't work....
So you'll need to fix this here first:
SET #imgName = 'SELECT (items) FROM dbo.building_piclink'
First of all - loose the single quotes:
SELECT #imgName = (items) FROM dbo.building_piclink
But now - do you have a unique ID that you can select for? Or do you want to get just the first entry (whatever that is) ??
So either you need:
SELECT #imgName = ImageFileName FROM dbo.building_piclink WHERE ..........
and fill in that WHERE clause with a condition that guarantees to return just a single row, or use TOP 1:
SELECT TOP (1) #imgName = ImageFileName FROM dbo.building_piclink
In that case - you'll just get exactly one filename - if you don't specify an ORDER BY, then there's no guarantee what you'll get - maybe you'll want to add a ORDER BY DueDate or something to prioritize which file names you get first.
2) Your code for loading the image data is non workable, either - what you need to do is build up the SQL statement as a string, and then execute it (called dynamic SQL) - something like this:
SET #imgString = 'C:\Documents and Settings\Administrator\Desktop\photos\' + #imgName
SET #insertString =
'INSERT INTO dbo.building__ATTACH (DATA)
SELECT * FROM OPENROWSET(BULK N''' + #imgString + ''', SINGLE_BLOB) as tempImg'
EXEC(#insertString) -- actually execute your SQL statement!
With these two fixes, you should be on the way to get this thing working