ASP.NET Update Command and SQL Logic - mysql

I am attempting to use the update command on an ASP.NET website and my logic of SQL follows:
UPDATE (SELECT `Event Name`,`Date`,`Time`,`Location`,`Goal`,`ID` FROM Calendar)
but, MySQL returns an error:
#1248 - Every derived table must have its own alias
Any ideas to fix this would be appreciated. Also, I have seen this alternative to plain SQL commands: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datagrid.updatecommand.aspx
All solutions would be helpful in structuring this ASP site (C#). Please keep in mind I am new to this technology.

The update statement looks a bit weird. Shouldn't it something like this:
UPDATE Calendar
SET [Event Name] = #Event, Date = #date, Time = #time,
Location = #location, Goal = #goal
WHERE ID = #id
Watch the spaces in column names, you should surround them with square braces, i.e. [Event name].

Related

Using concat to put together a simple string, a subquery, and another simple string

I am needing to use concat to create custom sql statements.
I'm trying to use
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1), 'nam.jpg')
The goal is to update the image_loc in setuptable to "/file/to/image/UIDnam.jpg"
I'm getting Syntax errors and "Expected End of Statement".
How can i tie a simple string, a subquery, and another simple string together to update a field?
I found a way to make it work. If any one has any tips of improvement, I'm up to learn!
*I can't use variables
update setuptable set image_loc = CONCAT('/file/to/image/', (select UID from table1)+, 'nam.jpg')
I found slightly better following structure :
update setuptable
set image_loc = CONCAT('/file/to/image/', t1.UID, 'nam.jpg')
from table1 t1
I don't have mysql server at hand for the moment, so I have not tested.

Change database based on parameters in SSRS

Im trying to retrieve data from our SQL server with the Report Builder. Since we have different databases for different customers I want to switch the database, the table I query has the same name in every database.
The idea I have is to use a parameter, so this is what I came up with:
use #db
Select H.TK_RC, R.Result_Description, count(*) as aantal from dbo.TK_HISTORY as H
LEFT JOIN CWESystemConfig..Result_Code as R on H.Project_ID = R.Project_ID and H.TK_RC = R.Result_Code
Where R.Result_Group = 1 and H.Project_ID = #pid and H.TK_CD between #startdate and #enddate
group by H.TK_RC, R.Result_Description
The code itself is working, the problem I encounter is when I create a dataset in the report builder. I think it's because the report builder can't find a startingpoint(of course) it keeps throwing the "define query parameters" pop-up, whatever I do it gives me the error:
Could not update a list of fields for the query. Verify that you can connect to the datasource etc.
If I remove the use #db it does work, so this is the issue. I can see the issue, since this is a variable the report builder doesn't see a valid query.
I just can't get this to work, anyone with suggestions for a fix or a workaround?
You should be able to do this by using an expression as your datasource connection string. There is no need to use the USE statement in your dataset query.
For a simple connection to SQL Server you would use something like
="data source=mySQLServer;initial catalog=" & Parameters!db.Value
Where your #db parameter has the name of the database
NOTES:
You must use an embedded datasource - you cannot expressions in a shared datasource
You should design and test the report using a fixed connection, then once your are happy with it, change the connection to use an expression.
Here's an screen shot of what you should be looking at

Update Case-Sensitive DB Field In Laravel 5.3 With Postgres

I am trying to update a database column field with raw SQL in laravel. It's important to mention that the update code was written to MySQL drive but now I use Postgres. The column name is dayID. So the update code is:
DB::update("update table set travel = ... WHERE dayID = {$this->dayID}");
I must use raw SQL because I make some updates to polygon types.
The problem is that laravel automatically transforms the dayID to dayid so I get an error:
column "dayid" does not exist
I tried to set a variable in order to use it in update query but it also failed with the same error:
$var = "dayID";
DB::update("update table set travel = ... WHERE ".$var." = {$this->dayID}");
How can I fix it?
Please try DB::table with update below:
DB::table('table_name')
->where('dayID', $this->dayID)
->update(['travel' => '...']);
Laravel document :
https://laravel.com/docs/5.3/queries#updates

How to update a row in a MySQL database using Ruby's Sequel toolkit?

This should be the simplest thing but for some reason it's eluding me completely.
I have a Sequel connection to a database named DB. It's using the Mysql2 engine if that's important.
I'm trying to update a single record in a table in the database. The short loop I'm using looks like this:
dataset = DB["SELECT post_id, message FROM xf_post WHERE message LIKE '%#{match}%'"]
dataset.each do |row|
new_message = process_message(row[:message])
# HERE IS WHERE I WANT TO UPDATE THE ROW IN THE DATABASE!
end
I've tried:
dataset.where('post_id = ?', row[:post_id]).update(message: new_message)
Which is what the Sequel cheat sheet recommends.
And:
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
Which should be raw SQL executed by the Sequel connector. Neither throws an error or outputs any error message (I'm using a logger with the Sequel connection). But both calls fail to update the records in the database. The data is unchanged when I query the database after running the code.
How can I make the update call function properly here?
Your problem is you are using a raw SQL dataset, so the where call isn't going to change the SQL, and update is just going to execute the raw SQL. Here's what you want to do:
dataset = DB[:xf_post].select(:post_id, :message).
where(Sequel.like(:message, "%#{match}%"))
That will make the where/update combination work.
Note that your original code has a trivial SQL injection vulnerability if match depends on user input, which this new code avoids. You may want to consider using Dataset#escape_like if you want to escape metacharacters inside match, otherwise if match depends on user input, it's possible for users to use very complex matching syntax that the database may execute slowly or not handle properly.
Note that the reason that
DB["UPDATE xf_post SET message = ? WHERE post_id = ?", new_message, row[:post_id]]
doesn't work is because it only creates a dataset, it doesn't execute it. You can actually call update on that dataset to run the query and return number of affected rows.

Parameters in SQL Server 2008

I have a stored procedure that pulls data for a report. I'm having a problem with the parameters. I have a couple temp tables and some joins that work so I have omitted them below. The problem is this line:
WHERE
SeminarDivision = #SeminarDivision AND SeminarType = #SeminarType
When I put this where clause in to use my seminar parameters the stored proc returns nothing But I need to generate a report based on those two parameters. So where do the parameters go? Can anyone help?
#StartDate DateTime,
#EndDate DateTime,
#SeminarDivision VARCHAR(50),
#SeminarType VARCHAR(50)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
... OMITTED
SELECT
WL.PID,
CONVERT(varchar(20), upper(substring(FirstName,1,1))+
LOWER(substring(FirstName,2,19))) AS FirstName,
CONVERT(varchar(20), upper(substring(LastName,1,1))+
LOWER(substring(LastName,2,19))) AS LastName,
S.SeminarDivision,
S.SeminarType,
S.StartDate,
S.SeminarLocation
FROM
#tblWaitList WL
INNER JOIN #tblSeminar S ON WL.SeminarGuid=S.SeminarGuid
WHERE
SeminarDivision = #SeminarDivision AND SeminarType = #SeminarType
ORDER BY
LastName,FirstName,StartDate
First and foremost there is nothing wrong with your code, when asking where do these parameters go, they go exactly where you put them. The question is - is the data coming in for SeminarDivision and SeminarType the right type of data? For instance just as a test,
copy the code into a new sql code query inside the editor. Run the command without the where, if you get values great. Now change the where to
WHERE
SeminarDivision = "Possible_Value"
Where Possible_Value should be a possible value...If it returns rows, good...now add the second condition also hardcoding a value:
WHERE SeminarDivision = "Possble_Value" AND SeminarType="Possible_Value_2"
Getting any data? Is it possible you want OR rather then AND ?
There's nothing wrong with the 'location' of your params.
If you're getting no data back, it's either because you've not populated #tblWaiList or #tblSeminar or because the records simply don't match your WHERE clause.
Check your params have the value you think they do by executing print #SeminarDivision etc.
SELECT * FROM #tblSeminar may give you a clue too.
You are not setting parameters correctly for the call.
Try this in SSMS, change values accordingly
EXEC Proc '20110101', '20111101', 'PossibleDivision', 'PossibleType'
If this fails, then show us "OMITTED" code
if this works, show us how you are calling this from the client code