How to update column in SQL Server 2008 - sql-server-2008

I'm new to SQL Server so any help with this would be greatly appreciated. I can not find the error while update the existing column with autogenerated record
Here is my table structure:-
CREATE TABLE [dbo].[EmployeeRegistration]
(
[ID] [int] NULL,
[Sr_no] [int] IDENTITY(1,1) NOT NULL,
[Branch_Code] [varchar](200) NULL,
[City] [varchar](200) NULL,
[Employee_Name] [varchar](200) NULL,
[Designation] [varchar](200) NULL,
[Department] [varchar](200) NULL,
[Date_of_Joining] [varchar](200) NULL,
[Reporting_To] [varchar](200) NULL,
[Staff_Code] [varchar](50) NULL,
[Reporting_To_1] [varchar](200) NULL,
[Staff_Code_1] [varchar](50) NULL,
[Emp_Staff_Code] AS ('T'+right('0000'+CONVERT([varchar](8),[Sr_no],(0)),(8))),
[Password] [varchar](50) NULL,
[confirmPassword] [varchar](50) NULL,
[EMAIL_id] [varchar](50) NULL,
[IsActive] [varchar](200) NULL,
[Created_By] [varchar](200) NULL,
[Created_On] [date] NOT NULL,
[Modified_By] [varchar](200) NULL,
[Modified_Date] [date] NOT NULL
)
I want to update Password column with below query:-
use AxpressDB
alter table EmployeeRegistration
alter column Password AS ('XP'+right('0123'+CONVERT([varchar](50),[Sr_no],(0)),(8)))

The syntax for the column altering :
ALTER TABLE tablename
ALTER COLUMN columnname columndatatype(size)
and the example :
ALTER TABLE dbo.doc_exy
ALTER COLUMN column_a DECIMAL (5, 2) ;
Try these manner

You cannot alter an existing column and make it as computed column, you need to drop and recreate
alter table EmployeeRegistration
drop column Password
alter table EmployeeRegistration
add Password AS ('XP'+right('0123'+CONVERT([varchar](50),[Sr_no],(0)),(8)))

Related

Using MySQL to create a DB and populate it with SQLAlchemy and Pandas

I made a Table Schema using MySQl Work Bench and want to populate it with tweets i scraped. All Foreign Keys and Primary Keys are set so far.
But i can´t get SQLalchemy to use the schema and populate it.
I tried it with this command line: user_df.to_sql(name='user', con=con, if_exists='append', index=False)
I shortend the code of the table creation to match only the "user" section.
The Error, which i get is
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1054, "Unknown column 'id' in 'field list'")
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema CapitolRiot
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `CapitolRiot` DEFAULT CHARACTER SET utf8 ;
USE `CapitolRiot` ;
-- -----------------------------------------------------
-- Table `CapitolRiot`.`User`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
`userName` LONGTEXT NULL,
`displayName` LONGTEXT NULL,
`description` LONGTEXT NULL,
`friendsCount` INT NULL,
`createdAt` DATE NULL,
`followersCount` INT NULL,
`statusesCount` INT NULL,
`favouritesCount` INT NULL,
`listedCount` INT NULL,
`mediaCount` INT NULL,
`location` LONGTEXT NULL,
`protected` TINYINT(1) NULL,
`linkUrl` LONGTEXT NULL,
`linkTcourl` LONGTEXT NULL,
`profileImageUrl` LONGTEXT NULL,
`profileBannerUrl` LONGTEXT NULL,
`url` LONGTEXT NULL,
`verified` TINYINT(1) NULL,
`userID` BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userID`))
ENGINE = InnoDB;
CREATE UNIQUE INDEX `userID_UNIQUE` ON `CapitolRiot`.`User` (`userID` ASC) VISIBLE;
So piecing it together, you're running:
user_df = pandas.json_normalize(user)
which is failing with:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1054, "Unknown column 'id' in 'field list'")
given that your user struct looks like:
{'username': 'EricFlut', 'displayname': 'Eric', 'id': 350130565, 'description': '', 'rawDescription': '', 'descriptionUrls': None, 'verified': False, 'created': '2011-08-07T07:38:54+00:00', 'followersCount': 149, 'friendsCount': 452, 'statusesCount': 756, 'favouritesCount': 141, 'listedCount': 7, 'mediaCount': 31, 'location': '', 'protected': False, 'linkUrl': None, 'linkTcourl': None, 'profileImageUrl': 'https://pbs.twimg.com/profile_images/1347294339672715267/HGJ4s-sv_normal.jpg', 'profileBannerUrl': None, 'url': 'https://twitter.com/EricFlut'}
And your table struct like:
CREATE TABLE IF NOT EXISTS `CapitolRiot`.`User` (
`userName` LONGTEXT NULL,
`displayName` LONGTEXT NULL,
`description` LONGTEXT NULL,
`friendsCount` INT NULL,
`createdAt` DATE NULL,
`followersCount` INT NULL,
`statusesCount` INT NULL,
`favouritesCount` INT NULL,
`listedCount` INT NULL,
`mediaCount` INT NULL,
`location` LONGTEXT NULL,
`protected` TINYINT(1) NULL,
`linkUrl` LONGTEXT NULL,
`linkTcourl` LONGTEXT NULL,
`profileImageUrl` LONGTEXT NULL,
`profileBannerUrl` LONGTEXT NULL,
`url` LONGTEXT NULL,
`verified` TINYINT(1) NULL,
`userID` BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`userID`))
I think that pymysql.err.OperationalError is originally getting thrown by mysql, python's just reporting it back to you. My guess is that mysql didn't know where to write the user.id field you provided - maybe because sqlalchemy didn't do its mapping correctly.
I know it's a compact document with a lot of content, but https://docs.sqlalchemy.org/en/13/orm/tutorial.html#declare-a-mapping is probably the best place to look.

MsSql Server , update query execution took too much time

I have a sql server db keeping our text document datas.
A specific table is keeping thousands of text paragraphs themself, This table suddenly started to respond so lately to update queries abnormally. Sometimes non-complicated basic update query takes 30 - 40 secs. to execute and asp.net application uses this table throws timeout exception.
Select queries last normal as before but update.
Why this happens and what should I do?
Server system is Windows 2008 R2 Standard and MsSql Server 2008 R2
Below's my table meta data.
CREATE TABLE [dbo].[MetinIcerikTablosuCari](
[ID] [int] IDENTITY(1,1) NOT NULL,
[MetinIsimID] [int] NOT NULL,
[MetinIsimID_Eski_1] [int] NULL,
[MetinIsimID_Eski_2] [int] NULL,
[MetinIsimID_Eski_3] [int] NULL,
[MetinIsimID_Eski_4] [int] NULL,
[MetinIsimID_Eski_5] [int] NULL,
[MetinIcindekilerID] [int] NOT NULL,
[SilmeTalebi] [bit] NULL,
[SiraNo] [int] NOT NULL,
[ParagrafMetin] [text] NOT NULL,
[RevizeParagraf] [text] NULL,
[RevizeVarMi] [bit] NULL,
[RevizasyonOnayi] [bit] NULL,
[RevizasyonTarihi] [datetime] NULL,
[RevizasyonSahibiID] [int] NULL,
[RevizasyonOnayTarihi] [datetime] NULL,
[RevizasyonOnaylayanID] [int] NULL,
[RevizasyonSonOnay] [bit] NULL,
[RevizeTuru] [int] NULL,
[ParagrafRevizeIslemJetonu] [nvarchar](50) NULL,
[BaglantiliOrtakRevizeSeriNo] [int] NULL,
[SonOnayTarihi] [datetime] NULL,
[SonOnaylayanID] [int] NULL,
[ParagrafMetin_Eski_1] [text] NULL,
[ParagrafMetin_Eski_2] [text] NULL,
[ParagrafMetin_Eski_3] [text] NULL,
[ParagrafMetin_Eski_4] [text] NULL,
[ParagrafMetin_Eski_5] [text] NULL,
[GirisTarihi] [nvarchar](50) NULL,
[YayinTarihi] [nvarchar](50) NULL,
[ParagrafNot] [nvarchar](max) NOT NULL,
[DipNotTag] [nvarchar](max) NOT NULL,
[MedyaURL] [nvarchar](100) NULL,
[MedyaUrlNot] [nvarchar](max) NOT NULL,
[VersiyonMetin] [int] NULL,
[EtkileyenMetinID] [int] NULL,
[tarih] [nvarchar](max) NULL,
[GuncellemeTarihi] [datetime] NULL,
[NotParagrafi] [bit] NULL,
[YeniParagraf] [bit] NULL,
[MedyaParagrafi] [bit] NULL,
[EslestirmeSilmeOnayID] [int] NULL,
[YargiOnBilgiSayfasi] [bit] NULL,
[BolumParagrafi] [bit] NULL,
[UstParagrafID] [int] NULL,
CONSTRAINT [PK_MetinIcerikTablosuCari] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_MetinIsimID] DEFAULT ((0)) FOR [MetinIsimID]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_MetinIcindekilerID] DEFAULT ((0)) FOR [MetinIcindekilerID]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_SiraNo] DEFAULT ((0)) FOR [SiraNo]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_ParagrafMetin] DEFAULT ('') FOR [ParagrafMetin]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_RevizeTuru] DEFAULT ((1)) FOR [RevizeTuru]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_ParagrafNot] DEFAULT ('') FOR [ParagrafNot]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_DipNotTag] DEFAULT ('') FOR [DipNotTag]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_MedyaUrlNot] DEFAULT ('') FOR [MedyaUrlNot]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_NotParagrafi] DEFAULT ((0)) FOR [NotParagrafi]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_YeniParagraf] DEFAULT ((0)) FOR [YeniParagraf]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_MedyaParagrafi] DEFAULT ((0)) FOR [MedyaParagrafi]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_EslestirmeSilmeOnayID] DEFAULT ((0)) FOR [EslestirmeSilmeOnayID]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_YargiOnBilgiSayfasi] DEFAULT ((0)) FOR [YargiOnBilgiSayfasi]
GO
ALTER TABLE [dbo].[MetinIcerikTablosuCari] ADD CONSTRAINT [DF_MetinIcerikTablosuCari_BolumParagrafi] DEFAULT ((0)) FOR [BolumParagrafi]
GO
I checked out and executed the update stats sp, but it seems the problem keeps going.
A simple update query like this: update MetinIcerikTablosuCari set ParagrafMetin = '<p>SPK Bültenleri - 2002/34: #media#93640#media#</p>' where ID = 35606 takes 20 - 30 secs or more.
I checked the CPU usage while the query executing, it suddenly picks to appr. 75 - 80%.
I could not determine if it's about lack of server memory or sql uses memory abnormally.
Below is my server hardware specifications:
Windows 2008 R2 Standard - Service Pack1
Processor: Intel Core i5-2400 CPU # 3.1 Ghz
RAM: 8GB
System Type: 64-bit
In addition, could this be kind of lack of updates for Windows Server or MsSql Server 2008 r2
As you haven't provided the execution plan and other necessary details, my assumption is that you might have indexes which are causing slow updates. Indexes helps running SELECT fast but slow down insert and update statements. List out all the indexes (Clustered & Non-Clustered) and then remove unnecessary indexes from your table.
Also, how often do you update statistics? If that is not recently done then run following SP
EXEC Sp_Updatestats;
If above two ways doesn't solve your problem then I recommend you to post table metadata and execution plan.

SQL Error : "Incorrect syntax near"

I have a database that is connected to SQL Server 2008, I am getting an error when printing database : Printing aborted! Error 37000 (Microsoft OLE DB Provider for ODBC Drivers) - [Microsoft] [ODBC SQL Server Driver] [SQL Server] [Incorrect syntax near 'TEMP_TAB_SHEET_U17'.
Details script as below :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [data].[TEMP_TAB_SHEET_U17](
[TAL] [varchar](30) NULL,
[Phase] [smallint] NOT NULL,
[SHT] [varchar](30) NULL,
[PreferedSYS] [varchar](30) NULL,
[sysdesc] [varchar](100) NULL,
[EquipmentNumber] [varchar](30) NULL,
[TaskNumber] [varchar](30) NULL,
[TaskId] [int] NULL,
[OTdescription] [varchar](50) NULL,
[OTremark] [varchar](50) NULL,
[Estimated_Man_Hours] [float] NULL,
[EquipmentId] [int] NULL,
[EquipmentDescription] [varchar](50) NULL,
[TAA_ID] [int] NULL,
[UserGroup] [varchar](30) NULL,
[MODULE] [varchar](30) NULL,
[ModuleDesc] [varchar](50) NULL,
[SHTTitle1] [varchar](50) NULL,
[SHTTitle2] [varchar](50) NULL,
[SHTTitle3] [varchar](50) NULL,
[SHTTitle4] [varchar](50) NULL,
[SHTDescription] [varchar](50) NULL,
[SHTNbSections] [int] NULL,
[NbMaxTasksPerSheet] [int] NULL,
[S1_PH] [varchar](30) NULL,
[S1_M] [varchar](30) NULL,
[S1_PF] [varchar](30) NULL,
[S2_PH] [varchar](30) NULL,
[S2_M] [varchar](30) NULL,
[S2_PF] [varchar](30) NULL,
[S3_PH] [varchar](30) NULL,
[S3_M] [varchar](30) NULL,
[S3_PF] [varchar](30) NULL,
[SHTFooterTitle1] [varchar](50) NULL,
[SHTFooterTitle2] [varchar](50) NULL,
[SHTFooterTitle3] [varchar](50) NULL,
[SHTFooterTitle4] [varchar](50) NULL,
[SHTFooterTitle5] [varchar](50) NULL,
[SHTFooterTitle6] [varchar](50) NULL,
[SHTFooterTitle7] [varchar](50) NULL,
[SHTFooterTitle8] [varchar](50) NULL,
[SHTFooterTitle9] [varchar](50) NULL,
[SHTFooterTitle10] [varchar](50) NULL,
[SHTFooterTitle11] [varchar](50) NULL,
[SHTFooterTitle12] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [data].[TEMP_TAB_SHEET_U17] ADD DEFAULT ((0)) FOR [Phase]
GO
Query completed running with the error :
Msg 2714, Level 16, State 6, Line 2
There is already an object named 'TEMP_TAB_SHEET_U17' in the database.
Msg 1781, Level 16, State 1, Line 2
Column already has a DEFAULT bound to it.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
How to solve this problem.
Thank you in advance.
Awan
Have you tried to set the constrain on creation:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [data].[TEMP_TAB_SHEET_U17](
[TAL] [varchar](30) NULL,
[Phase] [smallint] NOT NULL DEFAULT 0,
...
....
GO
SET ANSI_PADDING OFF
GO

SSIS get all data from A table that are not in B table and inserted it in table B

i have two table and i need to create SSIS package "data flow task"
table A and table B, i need to create SSIS package that always insert the data that are not in table B from table A into table B (based on badgeno in A and badgeno_ID in B ). any thought and helps plz?
CREATE TABLE [dbo].[A](
[firstname] [nvarchar](100) NULL,
[mobile] [nvarchar](50) NULL,
[note] [nvarchar](500) NULL,
[city] [nvarchar](255) NULL,
[badgeno] [nvarchar](50) NULL,
[lastname] [nvarchar](100) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[B](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](100) NULL,
[LastName] [nvarchar](100) NULL,
[DateOfBirth] [datetime] NULL,
[FirmID] [int] NULL,
[Phone] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[EMail] [nvarchar](100) NULL,
[EmployeeNr] [nvarchar](10) NULL,
[Note] [nvarchar](500) NULL,
[PhonePrivate] [varchar](50) NULL,
[StreetAddress] [nvarchar](255) NULL,
[Zip] [nvarchar](20) NULL,
[City] [nvarchar](255) NULL,
[Data] [nvarchar](500) NULL,
[UserName] [nvarchar](50) NULL,
[Password] [nvarchar](50) NULL,
[ADGUID] [nvarchar](50) NULL,
[Language] [nvarchar](50) NULL,
[Theme] [nvarchar](30) NULL,
[DefaultZone] [int] NULL,
[Bed] [nvarchar](50) NULL,
[badgeNO_ID] [nvarchar](50) NULL,
[RegisteredBy] [nvarchar](50) NOT NULL,
[Registered] [datetime] NOT NULL,
[LastUpdatedBy] [nvarchar](50) NOT NULL,
[LastUpdated] [datetime] NOT NULL,
[DefaultView] [nvarchar](50) NULL,
[ExpandTagList] [bit] NULL,
[PrintEmergencyReport] [bit] NULL,
[EmergencyReportPath] [nvarchar](255) NULL)
Drag and drop an Execute Sql Task and put the following t-sql query in there. Jobs a gooden.
INSERT INTO [dbo].[B]
([firstname] , [mobile], [note], [City], [badgeNO_ID] , [LastName])
SELECT [A].[firstname]
, [A].[mobile]
, [A].[note]
, [A].[City]
, [A].[badgeno]
, [A].[LastName]
FROM [dbo].[A]
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[B]
WHERE [dbo].[A].[badgeno] = [dbo].[B].[badgeNO_ID])
Use an SSIS Lookup Transformation with "No Match" Output
http://msdn.microsoft.com/en-us/library/ms141821.aspx
https://www.simple-talk.com/sql/ssis/implementing-lookup-logic-in-sql-server-integration-services/

Subquery returned more than 1 value in insert statement

I need solution to fix this error Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
I don't have subquery in my insert statement but still I am getting this error.
Code:
INSERT INTO enquiries_dtls(confdate,confname,region,enquiry,attendedby,dctname,
hospital,contactno,email,addr)
VALUES('10/04/2013',
'APAO',
'Delhi',
'test',
'test',
'test',
'test',
'123214',
'abc#abc.com',
'test')
DDL:
CREATE TABLE [dbo].[enquiries_dtls]
(
[sno] [int] IDENTITY(1,1) NOT NULL,
[Timestamp] [timestamp] NULL,
[confdate] [date] NULL,
[confname] [varchar](max) NULL,
[region] [varchar](50) NULL,
[enquiry] [varchar](max) NULL,
[attendedby] [varchar](50) NULL,
[dctname] [varchar](50) NULL,
[hospital] [varchar](50) NULL,
[contactno] [int] NULL,
[email] [varchar](50) NULL,
[addr] [varchar](max) NULL,
[stsupdate] [varchar](max) NULL,
[dtupdate] [date] NULL,
[closingdate] [date] NULL,
[clsedby] [varchar](50) NULL,
[revenue] [varchar](50) NULL,
[apprstatus] [varchar](50) NULL,
[clsed] [varchar](50) NULL,
[enqId] [varchar](50) NULL
);