how can I loop to update record in sql server - sql-server

I have database records to store page urls but we have problem in deployment the developer using localhost to store page filed in database because of that we end up with
http://localhost:8080/wow/page1.aspx
http://localhost:8080/wow/page2.aspx
http://localhost:8080/wow/page3.aspx
just wondering can i write Tsql to loop throw record to remove the localhost from url and updating the new url

You don't need a loop, a simple UPDATE will do the trick. E.g.
UPDATE WEB_TABLE SET URL_FIELD = REPLACE(URL_FIELD, 'localhost:8080', 'www.mysite.com')

You can execute a SQL update query using the replace method.
UPDATE [your table] SET [your field] = REPLACE([your field],'http://localhost:8080','')
That will update them all in one go.
OR, if you want to loop you could use a CURSOR

Related

[MS-Access][ODBC] Can't delete an line on a form, ODBC delete on a linked table failed

I have a form that is linked to an ODBC (MS SQLServer), that is displaying a result of a VIEW, when I try to delete a record, I'm invoking a function at VBA level
Form_BeforeDelConfirm(Cancel As Integer, Response As Integer){
If (IsNull(Text104.value) = False) Then
Dim deleteLabel As DAO.QueryDef
Set deleteLabel = CurrentDb.CreateQueryDef("")
deleteLabel.Connect = CurrentDb.TableDefs("KontrollWerte").Connect
If (InStr(QuerySave, "KontrollWerteVIEW") <> 0) Then
deleteLabel.sql = "delete from KontrollWerte_Label where Kontrolle_Label_ID = " & Text104.value
End If
Close
End If
}
Error shown:
[Microsoft][SQL Server Native Client 11.0][SQL Server] View or function 'dbo.KontrollwerteVIEW' is not updatable because the modification affects multiple base tables. (#4450)
This error is okay, as the view is a select with multiple tables.
It seems the function is not called at all, and the "default" delete function of the MS Access is being called, there is a way to say to MS Access don't do the default delete and instead execute my sql statement inside of the Form_BeforeDelConfirm function?
Thanks!
I tried to change when the call of function is called, but no luck.
You have to ensure that the view allows updates.
When you link a table, and it is a view?
The during the creating of the table link, then you will see this prompt:
First, we select the "view" when linking that "view/table"
and note I also checked the Save password option.
Then next we get/see this:
DO NOT skip the above prompt. If you don't select the PK column for that view, then it will be read only.
FYI:
The above prompt to save password, and the above prompt to select the PK when linking to that view?
It DOES NOT appear on re-link, ONLY when adding a new link!!!!
So, that's why I stated to delete the link to sql server (not just re-link and not just re-fresh table link, but Adding for first time are the key instructions here).
So, yes, views are and can be up-datable, but you MUST answer the above prompt during the linking process in Access, or the result is a read-only table.

Moving Access update query to SQL server

We are migrating a Access database to Azure, one update queries is not working despite trying several syntax changes and removals of spaces.
Below is the design view of the query in Access:
Query Design
The following is the SQL expression of the update query in Access:
UPDATE SPEND_steve, KeywordRULES
SET SPEND_steve.Category = [KeywordRULES].Category
WHERE (((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like "*"
And (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription]
And (SPEND_steve.ItemDescription) Like "*"));
With the above I receive error 102: Incorrect syntax near ','.
Thank you in advance for any help to move this functioning query from Access to SQL server!!!
Try removing ", KeywordRULES" from Line 1, an UPDATE statement is only able to update one table (or view) at a time.
UPDATE SPEND_steve
SET SPEND_steve.Category =
[KeywordRULES].Category WHERE (((SPEND_steve.Category) Is Null) AND
((SPEND_steve.ItemDescription) Like "" And (SPEND_steve.ItemDescription)=
[KeywordRULES].[ItemDescription] And (SPEND_steve.ItemDescription) Like "")) ;
SQL Update syntax is a bit different to Access, in that each update statement should affect a single table. You can however reference other tables through a join or other ways depending on what you are trying to do.
So KeywordRules is implicitly joined in your query. So your intention is to update the SPEND_steve table based on info from the KeywordRules table. You can do this by a join to the keyword rules.
Update SPEND_steve
Set SPEND_steve.Category = [KeywordRULES].Category WHERE
(((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like ""
And (SPEND_steve.ItemDescription) Like ""))
JOIN KeywordRules on (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription];
Also you should change like "" to = "" which may yeild a performance increase, although you might want to check for AND IS Not NULL as well if it could be null.

PostgreSQL query not working in Yii2

I want to execute query in my yii2 application. I'm using PostgreSQl. There is a table called list inside the user schema. If I try to build any query it returns 1. My code is here:
$numUsers = Yii::$app->db->createCommand('
SELECT COUNT(*) FROM "user"."list"
')->execute();
Please show me my mistake in the query above.
This is not related to the DB type in Yii2 if you want the result of a single value you should use queryScalar() instead of execute()
$numUsers = Yii::$app->db->createCommand('
SELECT COUNT(*) FROM "user"."list" ')->queryScalar();

SQL Server CE database and ADO.Net Entity Framework - data not saving

I have a SQL Server CE database file and I have ADO.NET Entity Framework object named History. I perform the following to get the latest ID:
var historyid = (from id in db.Histories // Get the current highest history Id
select (Int32?)(id.Id)).Max();
HistoryID = Convert.ToInt32(historyid);
if (HistoryID == 0) // No current record exists
{
HistoryID = 1; // Set the starter history Id
}
(Not the best way, but it is just to test).
This works well and returns the correct value (If I manually enter an ID of 1, it returns 1, same with 2 etc..)
The issue is with the following code:
History history = new History();
history.Id = 1;
history.SoftwareInstalled = "test";
db.AddToHistories(history);
db.SaveChanges();
The above works and runs through fine, but the changes are not saved to the database! Why is this?
Here is my db variable: dbDeSluggerEntities db = new dbDeSluggerEntities();
Thanks.
Edit: Here is an update: I noticed when I run the program and debug it, it shows that a history has been created and inserted and also gives an error of the primary key already existing, this happens each time I run the application (over and over). However, when I go to server explorer and right click the db and select 'Show Table Data' it shows no data.. When the program is run again, there is no primary key error or history's showing in the debug!
I was having exactly the same problem. By default the database is copied into your bin folder when you run the app and that is used.
My solution was to go into the properties of the db and set it to never copy and to put a full file path to the database in the connection string in the apps config file.

SQL Server Linked Server

All,
I am trying out a new linked server - I can run this command fine:
SELECT * FROM NextGen4.NGEPMWareHouse.dbo.Network_People
If I try to get fancy (update a table on my local server from the linked server)
UPDATE dbo.Network_People
SET dbo.Network_People.NGTimeStamp = NextGen4.NGEPMWareHouse.dbo.Network_People.[TimeStamp]
WHERE dbo.Network_People.HIN = NextGen4.NGEPMWareHouse.dbo.Network_People.HIN
I get
The number name 'NextGen4.NGEPMWareHouse.dbo.Network_People' contains more than the maximum number of prefixes. The maximum is 3.
Try woking with UPDATE … FROM and an alias.
UPDATE
dbo.Network_People
SET
NGTimeStamp = warehouse.[TimeStamp]
FROM
NextGen4.NGEPMWareHouse.dbo.Network_People AS warehouse
INNER JOIN dbo.Network_People AS people ON people.HIN = warehouse.HIN
Try to exclude ".dbo" prefix from your tablenames and use "update from"

Resources