Adding a new row Using SQL Server Management Studio? - sql

I'm learning how to use SQL Server Management Studio and can't figure out how to insert a new row into a table.
Table Structure:
ID,
Field1,
Field2
Query:
INSERT INTO Table (Field1,Field2) VALUES(1,2)
Error:
Major Error 0x80040E14, Minor Error 25503
I'm probably missing something very simple. Any help would be appreciated.

Ok, I was on the verge of pulling out all of my hair, and it appears using single quotes instead of double quotes fixed the problem.
Now, I want to pull my hair out even more.
Thanks for the replies everyone. This one was my mistake.

Does your table have an auto-incrementing ID field? If not, you will need to manually specify the value for the ID in your INSERT statement.
You can check if the ID field is auto-incrementing by using the Object Explorer, navigating to the table and expanding the Columns node. Find the ID column, right-click on it and select Properties. If the Identity property is set to False it means that the ID field is NOT auto-incrementing.
Your other option for adding a row to the table is to navigate to the table in Object Explorer, right clicking on it and selecting Open Table. You can then go to the last row in the grid and manually enter the values for the columns.

Related

Best way to duplicate data in the same table and updating a column at the same time

I'm trying to find the best way to duplicate all the rows in a table.By this i mean inserting them again in the same table, but i need to update a single column only on the values that were inserted.
This is to help me write a script so I can automate some work on some clients.
I can't use select * as it will throw an error because of the identity columns but at the same time i don't want to be manually writting all the column names for several tables.
Is there a simple way to translate this into SQL server?
Sorry for not showing a piece of code, but i have nothing at the moment and i'm not really fluent in SQL.
EDIT: I have ended up following the advice of JamieD77 in the comments below this post by moving everything to a table, drop the id column, updating what i need and then moving back as it seems to be the most effiecient.
In SQL Server Management Studio you can drag the "Columns" folder under a table and drop it on the query window and it will paste a comma-delimited list of all the columns.
Or run a query like:
select string_agg(quotename(name),', ')
from sys.columns
where object_id = object_id('MyTable')

ADO SQL query create column if it doesn't exist

I have a query for a report based on an MS Access database (as the program project file). The tables in this database get updated with new fields periodically as new features are added.
We need to be able to support old and new versions of the file for our report, so need to know if there is a way to insert a field into the SQL SELECT query if it does not already exist. (Note: Do not want to create ALTER TABLE type statements, as the field only needs to be added into the result set, not into the table permanently.)
I know you can do something like "" AS [FieldName], but that only applies when you know the field doesn't exist and need to create a blank spot for it (such as when a unioned table does have that field). In this case, the table might have the field so I want to use it if it does, but if it doesn't I want to have it still exist in the query results with a default value.
Any help would be appreciated. (I also know you can force the user to update the file, but that option was stated as "only last resort".)
Thanks,
Chris

how we I modify execute table and their fields in SQL server 2008 express database?

(after saving 1st error dialog box)
(after click on cancel)
(after click save text file as shown in 1st screen shot )
The problem is , when i add field or modify the datatype value for executed table it shows the error that database table can't modify.
Plz solve my problem. i face to much problem to add or to change something in field of any table.
when i do right click on any table , i can't get modify button..?
I had installed SQL Server 2008 Express edition.
Go to 'Tools' Menu
Select 'Options...'
From tree view at the left of window select 'Designers'
Clear three checkboxes:
'Warn about difference detection'
and 'warn about table affected'
and 'Prevent saving changes that require table re-creation'
Are you trying to modify the fields of your table?
Why doing it through Wizard when you can simply use the ALTER TABLE command to modify it.
But you must first make sure that there is no dependencies on other tables (foreign keys, etc.)
This link can help also.

Duplicating a TABLE using Microsoft SQL Server Management

Need to duplicate a TABLE using Microsoft SQL Management Studio 2008
The TABLE needs to duplicate all table row (Primary Key) ID as well.
In SSMS open a new query window and then do something like
SELECT * INTO NewTable
FROM OldTable
change NewTable to the name that the new table should have, change OldTable to the name of the current table
this will copy over the basic table structure and all the data...it will NOT do any of the constraints, you need to script those out and change the names in those scripts
An easy way to copy a table and all of it's data:
SELECT * INTO
[DATABASE_NAME].[SCHEMA_NAME].[NEW_TABLE_NAME]
FROM
[DATABASE_NAME].[SCHEMA_NAME].[OLD_TABLE_NAME]
The SCHEMA_NAME is often dbo
To duplicate a table and the data rows in the table, right-click on the database that contains the table you want to duplicate, then click 'Tasks' then 'Import Data...". See the screenshot below for visual representation. Then, follow the instructions in the "SQL Server Import and Export Wizard." Select the table to be duplicated as the 'source' and write in a made-up table name of your choice for the 'destination'. When finished on the last screen (see screenshot below), click 'Next', then 'Finish' and the Wizard will show you the progress of the data transfer until complete.
One way to copy the table structure (including default values) but NOT the actual table values is the copy / paste solution that is documented here. It works for Management Studio 2005 upwards. You just have to select all columns in the design then Edit -> Copy. Create a new table and the Edit -> Paste.

Is there a web site where I can paste a SQL Insert statement and have it break it out by column?

I've been working on an application that has no discernable data access layer, so all the SQL statements are just built up as strings and executed. I'm constantly confronted by very long INSERT statements where I'm trying to figure out what value in the VALUES list matches up with what column in the column name list.
I was about to create a little helper application where I could paste in an INSERT statement and have it show me a list of values matched up with the column names, just for debugging, and I thought, "someone else has probably done this already."
Does anyone know of a web site where I can just paste in an INSERT statement and have it show me a two column table with column names in the first column and values in the second column?
not a website but have you tried either visual studio or Sql Server Management studio. Both of these are able to do this.
You can use SQL Server Management studio or Visual Studio (with Server Explorer) to do that. For SSMS, follow these steps:
On a random table, click "Edit Top 200 Rows"
Open the SQL pane and paste your Insert script.
Open the Criteria Pane, which displays the insert column value pairs.
http://www.dpriver.com/pp/sqlformat.htm
I use Red Gate SQL Prompt for all my re-formatting.
Even though it is unnecessary (and unused), I often use aliases to make it easier to line things up, so instead of:
INSERT INTO tbl
SELECT 2 * y
FROM other_tbl
I usually do:
INSERT INTO tbl (x)
SELECT 2 * y AS x
FROM other_tbl