Perform Several Operations In 1 ALTER TABLE?

Mar 1, 2007

Hi there. Does anyone know of a why to perform several operations on a table within only 1 ALTER TABLE statement? I haven't found anything to date and don't even know if it's possible.

Thanks,

Angel

View 2 Replies


ADVERTISEMENT

SQL Server 2008 :: How To Perform Sum And Count Operations

Sep 9, 2015

I created a report where collected payments are listed by each collector from month to date.Everything Looks ok on the reporting side until a requirement came up where I am Also supposed to list the total number of payments collected by each collector (the count) and also the (total sum). If you look at the attached excel you would see two tables, I am getting the first table, 2nd is the one I want expected). How do I perform sum and count operations?

ALTER PROC USP_PaymentsByCollector
(
@CollectorName AS varchar(20) = NULL,
@CollectorCode AS varchar(20) = NULL,
@LoanID AS varchar (20) = NULL
--@StartDate AS DATETIME = NULL,
----@EndDate AS DATETIME = NULL

[code]....

View 3 Replies View Related

Alter Table Alter Column In MSACCESS. How Can I Do It For A Decimal Field?

Jul 23, 2005

Hi people,I?m trying to alter a integer field to a decimal(12,4) field in MSACCESS 2K.Example:table : item_nota_fiscal_forn_setor_publicofield : qtd_mercadoria integer NOT NULLALTER TABLE item_nota_fiscal_forn_setor_publicoALTER COLUMN qtd_mercadoria decimal(12,4) NOT NULLBut, It doesn't work. A sintax error rises.I need to change that field in a Visual Basic aplication, dinamically.How can I do it? How can I create a decimal(12,4) field via script in MSACCESS?Thanks,Euler Almeida--Message posted via http://www.sqlmonster.com

View 1 Replies View Related

Multiple Operations On A Common Table Expression

Jan 28, 2008

Hi,
I'd like to perform a number of different operations on my Common Table expression but I seem to be limited to only one operation. For example I cannot both delete duplicate rows and then perform a select statement. I can only execute one of the statements referencing the common table expression.

What is wrong with my syntax?


;With OrderedTable

AS

(

select Row_number() OVER (partition BY SSNumber order by Department_Id desc ) AS ROWID,* from Employee

)

delete from OrderedTable where RowId != 1

SELECT COUNT(*),SSNumber FROM OrderedTable group by Department_Id order by count(*) desc

View 1 Replies View Related

Alter Table Alter Column

Jul 20, 2005

I would like to add an Identity to an existing column in a table using astored procedure then add records to the table and then remove the identityafter the records have been added or something similar.here is a rough idea of what the stored procedure should do. (I do not knowthe syntax to accomplish this can anyone help or explain this?Thanks much,CBLCREATE proc dbo.pts_ImportJobsas/* add identity to [BarCode Part#] */alter table dbo.ItemTestalter column [BarCode Part#] [int] IDENTITY(1, 1) NOT NULL/* add records from text file here *//* remove identity from BarCode Part#] */alter table dbo.ItemTestalter column [BarCode Part#] [int] NOT NULLreturnGOSET QUOTED_IDENTIFIER OFFGOSET ANSI_NULLS ONGOhere is the original tableCREATE TABLE [ItemTest] ([BarCode Part#] [int] NOT NULL ,[File Number] [nvarchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_File Number] DEFAULT (''),[Item Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Item Number] DEFAULT (''),[Description] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Description] DEFAULT (''),[Room Number] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Room Number] DEFAULT (''),[Quantity] [int] NULL CONSTRAINT [DF_ItemTest_Quantity] DEFAULT (0),[Label Printed Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Label Printed Cnt]DEFAULT (0),[Rework] [bit] NULL CONSTRAINT [DF_ItemTest_Rework] DEFAULT (0),[Rework Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Rework Cnt] DEFAULT (0),[Assembly Scan Cnt] [int] NULL CONSTRAINT [DF_ItemTest_Assembly Scan Cnt]DEFAULT (0),[BarCode Crate#] [int] NULL CONSTRAINT [DF_ItemTest_BarCode Crate#] DEFAULT(0),[Assembly Group#] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Assembly Group#] DEFAULT (''),[Assembly Name] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULLCONSTRAINT [DF_ItemTest_Assembly Name] DEFAULT (''),[Import Date] [datetime] NULL CONSTRAINT [DF_ItemTest_Import Date] DEFAULT(getdate()),CONSTRAINT [IX_ItemTest] UNIQUE NONCLUSTERED([BarCode Part#]) ON [PRIMARY]) ON [PRIMARY]GO

View 2 Replies View Related

Alter Table Alter Column...

Oct 8, 2007

I am using sql server ce.I am changing my tables sometimes.how to use 'alter table alter column...'.for example:I have table 'customers', I delete column 'name' and add column 'age'.Now I drop Table 'customers' and create again.but I read something about 'alter table alter column...'.I use thi command but not work.I thing syntax not true,that I use..plaese help me?



my code:
Alter table customers alter column age

View 7 Replies View Related

How To Perform Table JOIN?

May 5, 2008

Hi all,
Lets say that i have 3 tables, they are already related together, information on each of the 3 tables and their relationship between eachother can be found below:I am using Microsoft SQL Server 2005 Database in Visual Web Developer 2008 using VB programming (ADO.NET)The 3 tables are as follows:
 
FloorRoomLocation
 
Fields inside each of the tables can be found below:

Floor Table(PK) ID(Required) NameDisplayOrder
Room Table(PK) ID(Required) Name(PK, FK) FloorIDExchangeIDExtensionIPDeviceNameRemarks(Required) Equipment(Required) Floor2IDFloorLocationXFloorLocationY
Location Table(PK,FK) FloorID (Required) CountryName(Required) CityName(Required) BuildingName
 
The Relationships between the 3 tables are as follows:
 
ID field in Floor table is referenced by the field named FloorID in the Room Table
ID field in Floor table is referenced by the field named FloorID in the Location table
 
NOTE: Location Table serves only as an extension to Floor table thus they have a 1 to 1 relationship, due to My Projects Lead's instructions I am not allowed to consolidate the Floor Table and the Location Table as 1 whole table.
Floor Table has a 1 to Many Relationship with the Room Table.
 
I Have already done the physical relationships between each of the 3 tables in the Database Diagram, in Visual Web Developer 2008 Express Edition under the Database Explorer
 
So i have created this form to accept user input:
 
this form is in a aspx page called AddRooms.aspx
 
Country Name:______________ (data sent to location table)
 
City Name:_________________ (data sent to location table)
 
Building Name:______________ (data sent to location table)
 
FloorID:[This is a DropDownList which is databinded to the Floor Table]
 
Room Name:_______________ (data sent to room table)
 
*Just imagine that the above is a form to accept user input
 
*With a hit of a submit button all data will be sent to their respective tables in the database
 
Now i know that i have to JOIN tables in my database in order for my application to work, but i have no experience performing JOIN, i need great guys like you to help me apply JOIN to the 3 tables mentioned above, do let me know if you have any doubts in what i am trying to do.
 
Do give me that code that i can either apply in the (VB environment Visual Web Developer 2008) and/or in Microsoft SQL Server 2005 environment
 
Just need the SQL Statment that i will need to construct to perform the JOIN i need to.
 

Please Help
 
Many Thanks : )

View 2 Replies View Related

TSQL - Using ALTER TABLE - ALTER COLUMN To Modify Column Type / Set Identity Column

Sep 7, 2007

Hi guys,
If I have a temporary table called #CTE
With the columns
[Account]
[Name]
[RowID Table Level]
[RowID Data Level]
and I need to change the column type for the columns:
[RowID Table Level]
[RowID Data Level]
to integer, and set the column [RowID Table Level] as Identity (index) starting from 1, incrementing 1 each time.
What will be the right syntax using SQL SERVER 2000?

I am trying to solve the question in the link below:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2093921&SiteID=1

Thanks in advance,
Aldo.

I have tried the code below, but getting syntax error...



ALTER TABLE #CTE
ALTER COLUMN
[RowID Table Level] INT IDENTITY(1,1),
[RowID Data Level] INT;


I have also tried:

ALTER TABLE #CTE
MODIFY
[RowID Table Level] INT IDENTITY(1,1),
[RowID Data Level] INT;







View 18 Replies View Related

Perform A Query That Will Merge Contents Of Various Table

Aug 27, 2012

I am trying to perform a query in SQL that will merge the contents of various table, using search criteria to narrow down the results. All of the tables have the exact same fields, and the search criteria is the same for all of them. What I am looking for is the most efficient way to perform this. I am using SQL stored procedures, and passing the results into my code. Note that in my real-world scenario there are more than three tables.

1) Merge all the tables into one and then perform the search criteria:
SELECT a, b, c FROM
(SELECT a, b, c FROM Table1
UNION ALL
SELECT a, b, c FROM Table2
UNION ALL
SELECT a, b, c FROM Table3) AS MY_MERGE
WHERE {SEARCH CRITERIA}

2) Perform the search criteria on each table and merge the results
SELECT a, b, c FROM
(SELECT a, b, c FROM Table1 WHERE {SEARCH CRITERIA}
UNION ALL
SELECT a, b, c FROM Table2 WHERE {SEARCH CRITERIA}
UNION ALL
SELECT a, b, c FROM Table3 AS MY_MERGE WHERE {SEARCH CRITERIA})

View 3 Replies View Related

Power Pivot :: How To Perform Lookup On A Related Table

May 12, 2015

Here is the data:

ProjectId
Stage
StateStatus
ProjectId
ProjectName
StartDate

P1
S1
Completed
P1
P1Name
31/12/2015

[Code] ...

I want with PowerPivot to tell what stage is in progress for project. I looked at RElatedTable, LOOKUPVALUES, but I can't find a way to associate this to get working. I got however the one telling me which projects are Completed.

Result shall be:

ProjectName StageinProgess
P1Name S2
P2Name S3
P3Name None

View 4 Replies View Related

Passing Parameter To OLE DB Command To Perform Update On Paradox Table

Aug 23, 2007



Dear All -
iam facing a problem for passing a parameter to update paradox table statment in the OLE DB Command

Briefly an OLE DB Command exist between OLE DB Source and OLE DB destination , all i want it to send the data that flow from the OLE DB Source to the update paradox table statment in OLE DB Command ,

i use the parameter annotation which is "?" to refer to the parameter in the update where clause and it sucess when i use the OLE DB Command to connect to SQL Server DB but when i use it with the Paradox Table an error appear saying that there is more than one data source column with the name "?" The data source column names must be unique.

i dont know what that means is it means that "?" it consider "?" as a column or what or is there another way to send parameter to Update statment for the paradox table differ from the SQL Server Update statment which is the "? "

i hope someone help me as soon as possible because i need it in work sOOon
Thanks,
Maylo

View 2 Replies View Related

How To Perform SELECT Query With Multiple Parameters In A Single Field In A Table

Sep 13, 2006

i just can't find a way to perform this Select Query in my ASP.Net page. I just want to find out the sales for a certain period[startDate - endDate] for each Region that will be selected in the checkbox. Table Sales Fields:       SalesID | RegionID | Date | Amount   This is how the interface looks like.Thank You.

View 1 Replies View Related

SQL Server 2008 :: Perform Checksum Based On The Source Table Data?

Aug 10, 2015

I'm trying to load data from old SQL server 2000 to new SQL server 2014. I need to do a checksum to check if all the source data is loaded in the target database(SQL server 2014). I've created the insert statement for the same which works. I need to use checksum to make sure all the source rows are loaded in the target table. I haven't done checksum before.

Here is my insert statement:

INSERT INTO [Test].[dbo].[Order_tab]
([rec_id]
,[date_loaded]
,[Name1]
,[Name2]
,[Address1]
,[Address2]

[code]....

View 2 Replies View Related

Transact SQL :: How To Alter Existing Table Column As Identity Without Dropping Table

Nov 20, 2013

I have created a table as below mentioned. Then I want to alter the ID column as identity(1,1) without dropping the table as well as losing the data.

create table dbo.IdentityTest
(
id int not null,
descript varchar(255) null,
T_date datetime not null
)

View 7 Replies View Related

Transact SQL :: Dynamically Alter Schema Of Stage Table When Source Table Structure Changed?

Oct 25, 2015

we have a table in our ERP database and we copy data from this table into another "stage" table on a nightly basis. is there a way to dynamically alter the schema of the stage table when the source table's structure is changed? in other words, if a new column is added to the source table, i would like to add the column to the stage table during the nightly refresh.

View 4 Replies View Related

How To Alter The Table With Delete/update Cascade Without Recreating The Table

Jul 26, 2004

I have contract table which has built in foreign key constrains. How can I alter this table for delete/ update cascade without recreating the table so whenever studentId/ contactId is modified, the change is effected to the contract table.

Thanks


************************************************** ******
Contract table DDL is

create table contract(
contractNum int identity(1,1) primary key,
contractDate smalldatetime not null,
tuition money not null,
studentId char(4) not null foreign key references student (studentId),
contactId int not null foreign key references contact (contactId)
);

View 3 Replies View Related

How To Alter Column Length Of The Master Table Along With The Slave Table?

Aug 13, 2006

In SQL Server 2005,here are two tables, created by the following SQL Statements:

CREATE TABLE student(
ID CHAR(6) PRIMARY KEY,
NAME VARCHAR(10),
AGE INT
);

CREATE TABLE score(
ID CHAR(6) PRIMARY KEY,
SCORE INT,
FOREIGN KEY(ID) REFERENCES student(ID)
);

For the length of Column ID is not enough, So I want to alter its length.The alter statement is:

ALTER TABLE student ALTER COLUMN ID CHAR(20)

For the table student is referenced by table score, the alter statement can not alter the column of the table student, and the SQL Server DBMS give the errors.

But, I can manually alter the length of the column ID in SQL SERVER Management Studio. How to alter column length of the master table(student) along with the slave table(score)?

Thanks!

View 2 Replies View Related

SQL Server 2008 :: Alter Table Triggers Recreate Table?

May 26, 2015

which ALTER TABLE/ALTER COLUMN- Statement has a Recreate Table as result ?

View 2 Replies View Related

How To Modify Colum (alter Table) Of Table That Is Replicated?

Jul 20, 2005

Hello!I have an MS SQL-server with an database, that runs replication. In thisdatabase there is an table with an columni want to extend; varchar(50)->varchar(60).But I get this error (using design window of Enterprise Manager): Cannotdrop the table 'MytableName' because it is being used for replication.Thanks for helpBjoern

View 1 Replies View Related

ALTER TABLE SWITCH PARTITION On A Replicated Table

Apr 10, 2007

A customer wants to implement table partitioning on a replicated table.



They want to hold 13 months of data in the table and roll off the earliest/oldest month to an identical archive table. The table has a date field and partitioning by month makes sense all around.



So SWITCH PARTITION is the obvious solution to this, except for the fact that the table is replicated (transactional w/no subscriber updates).



What are his architectural or practical solutions to using table partitioning and replication?











thx

View 5 Replies View Related

Alter A Table All In C#

Feb 15, 2006

Is there a way to add a column to an existing table and do it all in C#If my query string is as follows how do I execute the query?ALTER TABLE interests ADD COLUMN Swim VARCHAR(1) NOT NULL DEFAULT('n')ThanksMoonWa

View 2 Replies View Related

ALTER TABLE

Aug 13, 2002

I think I have blown a gasket..... a elementary simple sp that will not work but I am unable to find an explanation as to why..... please help.


CREATE PROCEDUREbrsp_table_alteration
@var_1 varchar(45)
,@var_2varchar(45)
,@var_3varchar(30)
AS

ALTER TABLE @var_1
ADD@var_2@var_3NULL


Thanks.

View 3 Replies View Related

Alter Table ......

Sep 30, 2003

I have a table tblABC which exists in 800 databases. I want to run a cursor to turn off the pk field ID indentity permannetly and then to "insert tblABC select * from tblXYZ".

I have tried the "set indentity_insert tblABC on" statement and failed on insert tblABC select * from tblXYZ.

Is there any alter table related t-sql could permannetly turn off the identity?

thanks
David

View 7 Replies View Related

Alter Table

Jun 7, 2001

Is there anyway to Insert a new column between two existing columns without creating a new table?

Thanks

View 3 Replies View Related

Alter Table

Oct 17, 2001

Hi,
is it possible to override the system when adjusting
columns. i.e. a system override setting.

For instance if I've got a column that is indexed and I
want to adjust it from varchar(50) to varchar(51) it lets
me. But if I then try and adjust the column to varchar
(49) I get the error below:

Server: Msg 5074, Level 16, State 8, Line 1
The index 'LastName' is dependent on column 'lastname'.
Server: Msg 4922, Level 16, State 1, Line 1
ALTER TABLE ALTER COLUMN lastname failed because one or
more objects access this column.

I asked this question on another Forum and was told I had to drop the index first. Just thought I'd ask for a second opinion.

Thanks for the help.

Steve

View 1 Replies View Related

Alter Table

Jan 30, 2004

Hi to all!
I have a silly problem, but I'm not able to solve it!
I know how to add a field in a table via Enterprise manager and I know also via ALTER TABLE command.
...But the problem is: I want to add a field NOT in the last position, but on the middle (for istance)
So, this is possible via Enterprise Manager but with the ALTER TABLE command? How can I add a field without put it in the last position?
...Thanks a lot!
Sergio

p.s.: Sorry for my poor english..I hope you understand my question!

View 2 Replies View Related

Can I Alter A Table Specified By @name?

Jun 5, 2008

hello,
i am trying to add a column to every table in a database, if it (doesn't already exist), with the following code:

declare @name varchar(50)
declare c cursor for select name from sys.tables
open c
fetch next from c into @name
while @@FETCH_STATUS = 0
begin
if( (select count(*) from sys.columns
WHERE object_id IN (SELECT object_id FROM sys.tables WHERE name = @name )
and name='ins_id') = 0 )
begin
alter table @name add ins_id int
end

fetch next from c into @name
end
close c
deallocate c

and when i run the script i get the error message
Incorrect syntax near '@name'.
reffering to the alter table statement
So, my question is, how can i actually perform the alter table in this case?
thank you

View 3 Replies View Related

Alter Table

Mar 2, 2007

Pramod writes "I want to change the primary key field of my database to Identity seed. the table is with data. data should be secured
this is the structure
'=================================
CREATE TABLE [tblStock] (
[StockId] [numeric](18, 0) NOT NULL ,
[ItemID] [numeric](18, 0) NULL ,
[Batch] [varchar] (50) NULL ,
[Qty] [float] NULL ,
[Prate] [float] NULL ,
[SRate] [float] NULL ,
[Mrp] [float] NULL ,
[PTR] [float] NULL ,
[batchid] [numeric](18, 0) NULL ,
[BatchCode] [varchar] (50) NULL ,
CONSTRAINT [PK_tblStock] PRIMARY KEY NONCLUSTERED
(
[StockId]
) ON [PRIMARY] ,
CONSTRAINT [IX_tblStock] UNIQUE NONCLUSTERED
(
[ItemID],
[Batch],
[Prate]
) ON [PRIMARY]
) ON [PRIMARY]
===================================
How do i use alter table with identity seed
ALTER TABLE tblStock alter column StockID Numeric IDENTITY (1, 1)

how the p[rimary key affects

Please help me"

View 1 Replies View Related

Alter Table Name

Sep 13, 2007

How to alter a table name by using Query Analyser

i tried like this..

ALTER TABLE of_info_grp
RENAME TO stp_info_grp_map

It is giving an error like this
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'TO'.

Some one help on this

View 2 Replies View Related

T-sql Alter Table Udf Help!

Jul 23, 2005

Hi,I'm trying to write a function check if a column exists on a table, andcreates it if it doesn't. The line that the query analyzer is citingis noted. It seems unhappy taking variables in the ALTER TABLEcommand. I can think of a hack around this, but I'm hoping there's abetter way? Muchas gracias in advance :)-DJCode followsCREATE FUNCTION fieldCreator(@TableName varchar(20),@FieldName varchar(20))RETURNS BITASBEGINif (EXISTS (SELECT count (objname)FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table',@TableName, 'column', @FieldName)GROUP BY objname))BEGINALTER TABLE @TableName ADD @FieldName int DEFAULT 4 --ERRORS HERE!!!EXEC sp_addextendedproperty 'caption', 'Created by script foranalysis', 'user', dbo, 'table', @TableName, 'column', @FieldNamereturn 1ENDreturn 0ENDGO

View 8 Replies View Related

Alter A SQL Table

Aug 18, 2006

I used the MS Access 2000 Upsize wizard to create a Client/serverapplication. After the wizard ran, the original table names had becomethe linked tables and then there were duplicates of the the tablesnamed xxx_local.I realized later that I needed to add some fields to a table andcouldn't do that in Access (couldn't edit the linked tables). So I usedthe Query Analyzer and created a script to add the fields to my table.It looked fine in Query Analyzer, but back in Access the fields wereadded to xxx_local and not xxx, the linked table. My forms and reportsare built on xxx, not xxx_local.How do I do this correctly?Jane

View 2 Replies View Related

Alter Table- ID

Jan 28, 2008

I am trying to insert some new rows in the table tbl_3, but the problem is that I don't know how to go along with the ID column. I want to insert new rows so that it gets appended at the end and new ID is automatically generated.
Looks like this had to be done while creating the table. The original table has the id assigned as 'Identity', but I have imported this table to my database and now it does not show that property in the ID column.

The Insert code that I am using is as follows and the tbl_3 has same no. of columns except it also has id.
Can I alter table without touching the previous IDs? The ids start at 28118 and ends at 28300.





Code Snippet
INSERT INTO [dbo].[tbl_3]
(3_name
,3_sort
,3_active
,3_display
,3_date)

select _MANUFACTURERNAME , 1, 1, 'True',
GETDATE()
from dbo._MANUFACTURERS where upper(_MANUFACTURERNAME) not in (
select Upper(3_name) from dbo.tbl_3 join
dbo._MANUFACTURERS on Upper(3_name) = Upper(_MANUFACTURERNAME))






And the error it is returning to me is

Msg 515, Level 16, State 2, Line 2

Cannot insert the value NULL into column '3_id', table '.dbo.tbl_3'; column does not allow nulls. INSERT fails.

The statement has been terminated.

View 2 Replies View Related

Alter Table

Mar 18, 2008

hi,

i have table having just two columns. One of columns is defines as nchar(5).

Want to alter table like
ALTER TABLE [dbo].[TestTable] ALTER COLUMN [Something] [nchar] (2)

COLLATE Latin1_General_CI_AS NOT NULL

GO
?

This script runs without problem till i have no data in table. When i add one row, this script fails with

Msg 8152, Level 16, State 13, Line 1

String or binary data would be truncated.

The statement has been terminated.


When i change the table in design mode, it says that data will be truncated and do the change (with truncating).

So is there any possibility (a switch or whatever) to update this table by script with letting engine truncate data?


Thanks for any response
Lubos

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved