I have a table in the database where it contains some of the user name. When I copy the database files from one machine to other machine, I need to manually update this table. The table contains the username in the following format
<domain name>\Username
the domain mostly the local machine name (the user exists within the system). What I am trying to write a simple SQL Query to find a pattern (machine name) and replace with a new one.
I am not so proficient with SQL queries. Can you share a sample snippet? I am using SQL Server 2008
UPDATE table_that_contains_users
SET field_user = replace( field_user, 'OLDDOMAIN\', 'NEWDOMAIN\')
is that?
Try this statement:
UPDATE your_table SET machine_name = REPLACE(machine_name, machine_name, 'Your New Value')
Related
I am uploading a data set from excel into out local server that we use to run offline queries and wish to use the information in that table to filter on a table that is in our production system so i can then pull through the filtered table onto out local DB rather than pull the entire table from prod to our local. I was looking at creating a variable that contained all the account numbers in a string and then use that in an IN(?) statement. This worked, but it only picked up the first account in the string and not the remainder.
I am unable to create a temp table in prod or anything as it is locked down for DBA only. I have tried some stuff in SSIS like variable creation etc but it hasn't worked. I am trying to avoid dumping the entire table. If I have to do this I will set it up to update from the valid from field to capture new records.
SELECT
*
FROM account_table
where [account number] in (?)
I have created the ? variable in an separate Execute task SQL box where the ? is
select
string_agg([account number], ', ')
from [distinct account_table].
I have also tried:
select
string_agg([account number], ',')
from [distinct account_table]
but that didn't work.
You can use expressions instead of passing parameters.
Open the Execute SQL Task editor, go to Expressions tab, add a new expression for the SqlStatementSource property as the following:
"SELECT
*
FROM account_table
where [account number] in ('" +
#[User::Variable] + "')"
On the other hand, if i am facing a similar situation i will use lookup transformation to do that instead of SQL statements.
First of all, linked servers is not needed here, it's merely a bridge that connects two servers.
I might be missing something obvious here.. You say that you've loaded the dataset to your local db and now you want to get the data from prod that contains those accounts.
My suggestion is to create a dataflow and use a oledb source to get the data from prod. Then add a lookup that joins to your data set in your local db (join on account, make sure that you connect to your local db and that you redirect no matching rows to no match output ).
Then add a oledb destination to load the data to your local db.
i am using new installed sql server 2008
i am trying to copy data from one table to another..i wrote query like this;
insert into Clr select * from Color_tbl
but this is showing error like this:
Invalid object name 'Clr'.
what i have to do? i have change any setting?
clr is not an existing table..i hop if i execute this query that will create clr table automatically...
I used to copy data from one table to another table like this: but i dont know what happend after installing new Sql server??
any help is very appriciable
Without knowing anything else about your database and schemas, you could try using fully qualified names, like so:
INSERT INTO [DBContainingClr].[SchemaContainingClr].Clr
SELECT * FROM [DBContainingColor_tbl].[SchemaContainingColor_tbl].Color_tbl
UPDATE: OP has clarified that he is in fact trying to CREATE the table Clr, and it doesn't exist at the moment. In which case the syntax should be:
SELECT * INTO Clr FROM Color_Tbl
Im trying to run the following to import a large volume of sales data in a text file into a database. When i run the following i get the error: "Invalid object name 'PetDatabase.Sales'
BULK INSERT PetDatabase.Sales
FROM 'C:\Temp\P1.txt'
WITH
(
FORMATFILE = 'C:\Temp\PetSales.Fmt'
);
Can anyone see whats causing my problem? I do have the tables within a folder; however, when i tried PetsDatabase.Tables.Sales it made no difference.
Ignore this answer. It was written when the question was tagged with mysql. Leaving the answer here to keep the comments.
--
Try using LOAD DATA INFILE instead.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Make sure PetDatabase.Sales exists in your text file.
Swap for whichever row and field terminator delimiters you're using. Here I'm using delimiters from a comma separated file
BULK INSERT PetDatabase
FROM 'c:\temp\p1.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM PetDatabase
GO
--Drop the table to clean up database.
SELECT *
FROM PetDatabase
GO
Also, make sure the following doesn't apply to you:
If a SQL Server user is logged in using Windows Authentication, the user can read only the files accessible to the user account, independent of the security profile of the SQL Server process.
When executing the BULK INSERT statement by using sqlcmd or osql, from one computer, inserting data into SQL Server on a second computer, and specifying a data_file on third computer by using a UNC path, you may receive a 4861 error.
To resolve this error, use SQL Server Authentication and specify a SQL Server login that uses the security profile of the SQL Server process account, or configure Windows to enable security account delegation.
Is PetDatabase is schema name or database name?
If it is database name, then include schema name also like this if your schema name is dbo.
PetDatabase.dbo.Sales
thanks for your time. i edited my script, ran it, and still got this name: srp.dbo.gstDataCutover. i used to be able to do this easily with MSSQL2005. we've recently upgraded to 2008. and i dont remember doing it any other way...
Hi,
I'm trying to copy a table structure (columns, datatypes, schema) into a new table to have the same schema and structure, using the sql code below.
SELECT dbo.gstData.*
INTO [dbo.gstDataCutover]
FROM dbo.gstData
WHERE dbo.gstData.gstID < 1
My problem is, when i run this script the new table dbo.gstDataCutover is named as "dbo.gstDataCutover" but the schema is defaulted to the system schema ("srp"), which is actually srp.[dbo.gstDataCutover].
I want to copy both the structure and the schema.
Thanks!
Without any periods, the hard brackets indicate table name -- it's including the "dbo." in your example as part of the table name.
If you want the table created in the dbo schema:
SELECT t.*
INTO dbo.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
Likewise, if you want the table created in the srp schema:
SELECT t.*
INTO srp.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
The table name doesn't have any unusual characters, so there's no need to use hard brackets...
You can download the community edition of Visual Studio, which has features for comparing schemas as well as data. It will list the differences and allows you to select a set of changes, for which it will generate an update-script.
I'm using ms sql 2008 and trying to create a database name that references another database. For example 'Dev', 'Test', 'Demo' would be database names that i could reference from my multiple config files, but each name would point to another database such as 'db20080101' or 'db20080114'.
[Edit]Some of the configs are for applications that i control the code and some aren't (ex. MS Reporting service datasource file configs)[/Edit]
It seems that sqlserver only supports synonyms for View,Table,Sproc, or Function. And Alias' are for table and column names.
Is there a way to do this that i missed in the docs?
Any one have any suggestions on a workaround?
use 3 part notation and alias up to the table, example
select * from tempdb.dbo.sysobjects a
join master.dbo.sysobjects b on a.id = b.id
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.
I've done something similar to this using another config file.
The new config file maps your generic name to all of the information needed to connect to that database (db name, user name, password, etc.) and then your connection function takes your generic name as an argument.
db.config:
DEV_DB_NAME = db20080101
DEV_DB_USER = dev_user
DEV_DB_PASS = dev_pass
TEST_DB_NAME = db20070101
TEST_DB_USER = test_user
TEST_DB_PASS = test_pass
connection code:
db_connection get_connection(string prefix) {
db_connection db_conn = new db_connection;
string db_name = get_config_value(config_path, prefix + "_DB_NAME");
string db_user = get_config_value(config_path, prefix + "_DB_USER");
string db_pass = get_config_value(config_path, prefix + "_DB_PASS");
db_conn.connect(db_name, db_user, db_pass);
return db_conn;
}
Then you just call get_connection() with your db alias as the argument.
I know this probably will not help in all situations, but you still have the option of using views. You can insert, delete, update, select into a view as long as it has a proper identity key (Primary Key). If you point it to another database, you should drop and recreate to get the different schema (in case you're working between production and test while making changes to the schema in test and/or production.
Synonyms are useful for when you're going to another database and have a 3 or 4 part name, but when you want to make it so you can have a set name, a linked server will also work which will let you use a fixed name if the table names are the same in both databases and you're just pointing between prod and test.