Altering a table on a different server through a query - sql

I'm writing some dynamic SQL (I'm fairly new to it) and am trying to automate the altering of multiple tables which may exist on different instances of SQL Server 2008. My servers are linked and I know which server each of the tables exists on, however when I try to run the query below, I get "Cannot find object...because it does not exist or you do not have persmissions"
Query:
Alter Table [Server10].[Database2].[dbo].[documents] Add NewField int
If I connect to the server in SSMS and drop the server name (Server10) it works.
Any suggests on how to create this query. Thanks

I believe that alter table on a linked server is not supported. You could do something like this:
EXECUTE [Server10].[Database2].[dbo].sp_executesql N'ALTER TABLE [documents] Add NewField int'

Related

Cant add table to database via linked server

I am trying to add a table to an existing database in a linked server, but getting:
The object name 'name.mycompany.com.DataAg.dbo.Secure30AWSMap' contains more than the maximum number of prefixes. The maximum is 2.
What am I doing wrong? Based on this link it seems like I cant use a 4 part name. However this link sounds like it might be a workaround using an Exec statement. Is there a way to solve this without messing with the linked servers DDL though?
create table [name.mycompany.com].[DataAg].[dbo].[Secure30AWSMap]([servername] [nvarchar](50),[username] [nvarchar](50),[full_name][nvarchar](50),[awscoid][nvarchar](50))
if RPC is configured true for linked server, it can help.
EXEC('create table [DataAg].[dbo].[Secure30AWSMap]([servername] [nvarchar](50),[username] [nvarchar](50),[full_name][nvarchar](50),[awscoid][nvarchar](50))') AT [name.mycompany.com]
You need to go to your linked server and manually create the table and then try this.
INSERT INTO [linkedserver].[database].[dbo].[table]
SELECT servername, username, fullname, awscoid
FROM [database].[dbo].[table]
WHERE ID = userID;

sql update data in 2nd db from 1st db only where table exists

I'm trying to move some tables from a live db to another db, which will then be made live after the data has been transferred. The 2nd db exists on a server with 2008, and the migration is from 2012. which has caused a few problems
Originally I did a full DB export with Scripts. because the export tool wouldnt go to previous versions. - after the full export I deleted the superfluous tables on db2. as I am only moving 40 or so out of about 200.
Now the DB structure is set up, all is working. with out of date-data. and everything is ready to be switched once the data is made up to date. So i'd ideally like a script that checks to see if a table in db1 exists in db2, then if it does to copy across all rows. Is this possible?
If creating a linked server is not an option then you can try using sql server import data wizard to transfer data from one db to another.
Use the following link for how-to:
Import data easily in sql server
You'll need to create a procedure on DB2 that returns a result based on the table you're checking:
USE [DB2]
CREATE PROCEDURE DBO.CHECKDB2TABLE
#TABLENAME NVARCHAR(50)
AS
IF EXISTS (
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = #TABLENAME)
)
BEGIN
SELECT 'TRUE'
ELSE
SELECT 'FALSE'
END
Then use the procedure to check DB2 and create a conditional statement based on that output to copy across the table.
EXEC DB2.dbo.CheckDB2Table #TABLENAME='Tablename'
Thanks.

Can an ALTER TABLE accept query results in Access 2010

I am getting a syntax error on the below code trying to execute an ALTER TABLE query in Access 2010. The ulimate goal was to execute this from a VB.net app. Both queries work indepently within Access.
ALTER TABLE [Test_table] DROP CONSTRAINT (SELECT [MSysRelationships].[szRelationship]FROM [MSysRelationships] WHERE MSysRelationships.[szObject]='Test_table');
I guess the issue is whether or not the ALTER statement can accept query results as the input?
I'm quite sure that Access SQL does not support the syntax you tried to use. You'll probably have to run the SELECT query first, pull the constraint names into a recordset (or similar), then loop through the rows and issue the ALTER TABLE statements one by one.

How To Get 'Create Table' script for a table or sp using vb6 with sql server 2000

I need the "create table... " statement for a particular table and stored procedure to recreate them in another database. Forget the backup and restore. I have to do it in vb6. Its the same thing you get when you copy the Table And paste it in query analyzer. Its sql server 2000
Edit: Now I Know it can be asked as 'How To Script Entire Database Through VB6'.
Take a look at this StackOverlow post which talks about the same. Although it is not VB6-specific, you should be able to apply this solution without a problem.
Essentially you create a stored procedure that will generate the CREATE TABLE statement for the given table. The stored procedure will examine the sysobjects table to build the SQL.
Your VB6 application can run the stored procedure on server 'A' fetching the CREATE TABLE SQL. Then connect to server 'B' and run that SQL to create the table on the other server.

SQL - create database and tables in one script

Sorry if already asked, but I can't find anything on this.
I am moving something over from MySQL to SQL Server I want to have a .sql file create a database and tables within the database. After working out syntax kinks I have gotten the files to work (almost).
If I run
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
it works fine, and if I run
CREATE TABLE dbname.dbo.TABLE1 (
);
...
CREATE TABLE dbname.dbo.TABLEN (
);
it also works fine. But, if I run them in the same file I get this error
Database 'dbname' does not exist
Right now, the CREATE TABLE statements are not within the IF statement, which I would like, but I also cannot seem to find the syntax for that. ( { } does not work?)
So my big question is, how do I ensure a particular command in a .sql file is completed before another in SQL Server?
My second question is, how do I include multiple instructions within an IF clause?
To be clear, I have been running this into sqlcmd.
Put a GO command between queries.
IF db_id('dbname') IS NULL
CREATE DATABASE dbname
GO
CREATE TABLE dbname.dbo.TABLE1 (
);
CREATE TABLE dbname.dbo.TABLEN (
);
As for putting the table statements in the IF, you wouldn't be able to because of the GO command. You could create additional IF statements afterwards, to check for each tables pre-existence.
The syntax for a block if is:
IF condition
BEGIN
....
....
END
Between creating the database and creating the tables you will need a USE statement.
USE dbname
This way the tables will be created in the correct place, without having to specify the DB name on everything.
Also, GO and BEGIN...END like everyone else is saying.
You have to separate the statements with the GO keyword:
sql query
GO
another sql query
GO
and so on
By placing a GO between statements (to create separate batches of statements)