I want to copy and rename a table in SQL EXPRESS in an mdf database. Which is the best way to do this?
Try a
select * into [newtablename] from [oldtablename]
which will make a copy of your old table with a new name.
You can rename a table using sp_rename. Note the warning (which will also appear when you run the stored proc):
Changing any part of an object name can break scripts and stored procedures.
Related
I have created a Pivot table using columns passed in from a variable. This variable is set based on a flag in another table, so the name and number of columns can change month-to-month. I need to be able to export this pivot to a delimited text file. The pivot table is stored in a temp table ##PivotTable
I have tried to do this via SSMS, but the only way I know how to do this is to define a file connection manager, which needs set columns. I also tried via sqlcmd, but that one exports extra information and spaces.
I am open to pretty much any process that would allow me to run this via a SQL Agent job on a monthly basis.
Any assistance would be much appreciated.
You can use SQL server import and export wizard
Right click on the database-->Tasks-->ExportData..
map your ##PivotTable.
Click next and set your source database (Select OLEDB provider for SQL Server)
Set a file name for your file (e.g. d:\monthlyData\202006\MyFile.csv
Select option copy data from one to more tables or views -- do not use this option due the fact that your columns getting changed.
Select option write a quarry
You can run this immediately or save as SSIS package and reuse it using SQL job
I think you'll have to use dynamic SQL like found here.
I am using SSMS and need to create a .bak of a table in one of the databases containing the content of that table.
The reason behind this is that I might need to populate the database again with this data at a later time (it is test data) and generating it again using the script I wrote takes to much time.
How do I do this in SSMS?
Taking your response to WEI_DBA very literally and accepting you prefer a bak file, I suggest you create a second database, then copy the content of this specific table (eg. with 'select into') to the second database. You can then backup the second database in the regular way.
create database foo
select *
into foo.dbo.table1
from dbo.table1
backup database foo
to disk = 'c:\temp.foo.bak'
with format
, medianame = 'foobak'
, name = 'Full backup foo'
The thing is that your instinct tells you that you need a backup (.bak) file. This is not the best way to share the contents of the table. Users would have to restore it to a dummy database and then copy in the same way as you now have ahead.
Sharing the table content via csv or xml is in my opinion a better way (and for that you can indeed use the Import/Export wizard as mentioned earlier).
Is there a nice way before I alter a table (e.g. remove a column), to see if that this will break any stored procedures?
I am trying to do this in MS SQL Server
Use the query here to search all stored procedures for the table and column name. You will probably still want to look at the code for each one you find to verify that it will or won't break.
you can use the following query to search for the table name in any stored procedures:
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%Your_Table_Name%'
I suggest you:
Make sure you have a separate environment (DEV)
Use the sample code from here to create a proc that confirms all objects in the database can be recompiled
How to Check all stored procedure is ok in sql server?
Use it - I can guarantee you will already have failing objects before you remove your column
Remove your column and use it again to see if more things broke
The more mature approach to this is to put your database into a database project and build that. But you can't do this until your database is valid.
I have one database with an image table that contains just over 37,000 records. Each record contains an image in the form of binary data. I need to get all of those 37,000 records into another database containing the same table and schema that has about 12,500 records. I need to insert these images into the database with an IF NOT EXISTS approach to make sure that there are no duplicates when I am done.
I tried exporting the data into excel and format it into a script. (I have doe this before with other tables.) The thing is, excel does not support binary data.
I also tried the "generate scripts" wizard in SSMS which did not work because the .sql file was well over 18GB and my PC could not handle it.
Is there some other SQL tool to be able to do this? I have Googled for hours but to no avail. Thanks for your help!
I have used SQL Workbench/J for this.
You can either use WbExport and WbImport through text files (the binary data will be written as separate files and the text file contains the filename).
Or you can use WbCopy to copy the data directly without intermediate files.
To achieve your "if not exists" approache you could use the update/insert mode, although that would change existing row.
I don't think there is a "insert only if it does not exist mode", but you should be able to achieve this by defining a unique index and ignore errors (although that wouldn't be really fast, but should be OK for that small number of rows).
If the "exists" check is more complicated, you could copy the data into a staging table in the target database, and then use SQL to merge that into the real table.
Why don't you try the 'Export data' feature? This should work.
Right click on the source database, select 'Tasks' and then 'Export data'. Then follow the instructions. You can also save the settings and execute the task on a regular basis.
Also, the bcp.exe utility could work to read data from one database and insert into another.
However, I would recommend using the first method.
Update: In order to avoid duplicates you have to be able to compare images. Unfortunately, you cannot compare images directly. But you could cast them to varbinary(max) for comparison.
So here's my advice:
1. Copy the table to the new database under the name tmp_images
2. use the merge command to insert new images only.
INSERT INTO DB1.dbo.table_name
SELECT * FROM DB2.dbo.table_name
WHERE column_name NOT IN
(
SELECT column_name FROM DB1.dbo.table_name
)
I got a table named tblHello and I wanna rename it to Hello
Right click on the table and select rename in management studio
You can also use sp_rename:
sp_rename 'old_table_name', 'new_table_name'
I want to point out that table renaming is not as simple as just changing the name when you have queries written. You also need to change all references to the old name in every stored proc, view, function and dynamic sql code. This is not something to be taken on lighty in something that is already on production.
But #jonH has the answer for how to do it (you run that in the query window making sure you switch to the correct database first). Of course you have to have the right security permissions to rename objects.
If its a small project,
you can directly change it from User Interface.
In Server Explorer, right-click the table you want to rename and Open Table Definition.
Right-click the table in the Table Definition window and choose Properties from the shortcut menu.
In the field for the Name value in the Properties, type a new name for the table.
Save the table.
And its done.