SQL Server Wizard says my source datetime's are datetime2's. Process cannot be run - sql-server-2008-r2

I am trying to export some records from ServerA.DatabaseA..Anchor to ServerB.DatabaseB..Anchor. The source is active. The destination is a development database. The destination has one additional field, a tenancy id.
SQL for Source table
CREATE TABLE [dbo].[Anchor](
[AnchorId] [int] IDENTITY(130,1) NOT NULL,
[NodeId] [int] NOT NULL,
[UnitName] [varchar](200) NOT NULL,
[SetPosition] [varchar](5) NOT NULL,
[CreateDate] [datetime] NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[Latitude] [float] NULL,
[Longitude] [float] NULL,
[RadiusMeters] [int] NOT NULL,
[Creator] [varchar](100) NULL,
[Cellnumber1] [varchar](20) NULL,
[Cellnumber2] [varchar](20) NULL,
[Cellnumber3] [varchar](20) NULL,
[EmailTo] [varchar](255) NULL,
[UseMsgFwdContacts] [bit] NOT NULL,
CONSTRAINT [PK_Anchor] PRIMARY KEY CLUSTERED
(
[AnchorId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Anchor] ADD CONSTRAINT [DF_Anchor_UseMsgFwdContacts] DEFAULT ((0)) FOR [UseMsgFwdContacts]
GO
SQL Query for selecting records
Datetime's are cast to datetime for testing
SELECT [AnchorId]
,'MXM' as OrgCode -- the new tenancy field
,[NodeId]
,[UnitName]
,[SetPosition]
,CAST([CreateDate] as DATETIME) as [CreateDate]
,CAST([StartDate] as DATETIME) as [StartDate]
,CAST([EndDate] as DATETIME) as [EndDate]
,[Latitude]
,[Longitude]
,[RadiusMeters]
,[Creator]
,[Cellnumber1]
,[Cellnumber2]
,[Cellnumber3]
,[EmailTo]
,[UseMsgFwdContacts]
FROM [client_Maxam].[dbo].[Anchor]
WHERE AnchorId >335
Click Ok and Next, and now...
Why is my source datatype being picked up as datetime2, preventing me from running the export.

Related

SQL Server issue with SELECT and datetime2 [duplicate]

This question already has answers here:
SQL Server Query: Fast with Literal but Slow with Variable
(8 answers)
Closed 10 months ago.
I found a big difference of the query execution under MS SQL Server Standart 2019.
T-SQL
DECLARE #atTime datetime2 = '2022-05-04 13:23:20';
DECLARE #startTime datetime2;
DECLARE #shiftTime datetime2;
SET #startTime = #atTime;
SET #shiftTime = DATEADD(SECOND, -5, #atTime)
-- SELECT #shiftTime, #startTime
-- 2022-05-04 13:23:15.0000000 2022-05-04 13:23:20.0000000
-- #1 It takes 7 seconds to complete
SELECT TOP(1) * FROM [TrackerPositions] WITH(NOLOCK) WHERE AtTime BETWEEN #shiftTime AND #startTime
-- #1 It takes 0 seconds to complete
SELECT TOP(1) * FROM [TrackerPositions] WITH(NOLOCK) WHERE AtTime BETWEEN '2022-05-04 13:23:15.0000000' AND '2022-05-04 13:23:20.0000000'
Note: AtTime colum has datetime2
Please, help to get working fast SELECT #1
Thank you!
UPDATE #1
CREATE TABLE [dbo].[TrackerPositions](
[ID] [uniqueidentifier] NOT NULL,
[GPSTrackerID] [int] NOT NULL,
[AtTime] [datetime2](7) NOT NULL,
[Lat] [decimal](9, 6) NOT NULL,
[Lng] [decimal](9, 6) NOT NULL,
[GeoLocation] AS ([geography]::STGeomFromText(((('POINT('+CONVERT([varchar](20),[Lng],0))+' ')+CONVERT([varchar](20),[Lat],0))+')',(4326))),
[SignalLevel] [int] NULL,
[IPAddress] [nvarchar](40) NULL,
[Port] [int] NULL,
[Height] [int] NULL,
[IsMoving] [bit] NULL,
[Speed] [decimal](18, 4) NULL,
CONSTRAINT [PK_TrackerPositions] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 50, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TrackerPositions] ADD CONSTRAINT [DF_TrackerPositions_ID] DEFAULT (newid()) FOR [ID]
GO
ALTER TABLE [dbo].[TrackerPositions] ADD CONSTRAINT [DF_TrackerPositions_IsMoving] DEFAULT ((0)) FOR [IsMoving]
GO
ALTER TABLE [dbo].[TrackerPositions] WITH CHECK ADD CONSTRAINT [FK_TrackerPositions_GPSTrackers] FOREIGN KEY([GPSTrackerID])
REFERENCES [dbo].[GPSTrackers] ([ID])
GO
ALTER TABLE [dbo].[TrackerPositions] CHECK CONSTRAINT [FK_TrackerPositions_GPSTrackers]
GO
The right answer is to use OPTION(RECOMPILE)
SELECT TOP(1) * FROM [TrackerPositions] WITH(NOLOCK) WHERE AtTime BETWEEN #shiftTime AND #startTime OPTION(RECOMPILE)

Why does Azure Data Flow convert varbinary(1000) to varbinary(max) and how can I prevent this?

My source table, which is located in an Azure SQL Server Data Warehouse, has a column named Upline with the data type varbinary(1000). In the destination table, located in the same Azure SQL Server Data Warehouse, the data type and column name are the same. My issue is in the Azure Data Flow that is populating the destination table.
Instead of inserting the data into the sink table in the data flow, it is creating a new table in my data warehouse. Here is the create statement for the table that is being created with
CREATE TABLE [Common].[T_7be15bb497654f0c8eeb82459912f178]
(
[EmployeeSK] [int] NULL,
[EmployeeLastName] [nvarchar](max) NULL,
[EmployeeFirstName] [nvarchar](max) NULL,
[EmploymentStatus] [nvarchar](max) NULL,
[HireDate] [date] NULL,
[OriginalHireDate] [date] NULL,
[TerminationDate] [date] NULL,
[CurrentPosition] [nvarchar](max) NULL,
[PreviousPosition] [nvarchar](max) NULL,
[WorkAssignmentEffectiveStart] [date] NULL,
[Region] [nvarchar](max) NULL,
[District] [nvarchar](max) NULL,
[Site] [nvarchar](max) NULL,
[OnSiteDepartment] [nvarchar](max) NULL,
[DepartmentName] [nvarchar](max) NULL,
[ManagerDayForceEmployeeNumber] [nvarchar](max) NULL,
[Upline] [varbinary](max) NULL,
[Lvl] [int] NULL,
[dimStartDate] [date] NULL,
[dimEndDate] [date] NULL,
[dimIsCurrent] [int] NULL,
[dimHash] [nvarchar](max) NULL,
[r7ace46966877481a90d6f8039c6524b5] [int] NULL
)
As you can see from the picture (if you can see the picture), the table is giving the column Upline a varbinary(max) data type. Why is this happening? How can I stop it from happening? When I take this column out of the source and destination tables it works successfully. However, I need the column. The data flow has these activities: source, select, derived column, surrogate key, and sink. It is doing very simple stuff and the Upline is not being changed in the derived column.
Here is the error message from the pipeline that runs the data flow:
"Found an implicit conversion from VarBinary(Max) to VarBinary(1000) that requires ANSI truncation warning. This is not supported. Use the CONVERT function explicitly to execute this request." There is no convert function in the derived column activity so I can't do the suggestion it gives.
The max length of the data in the column is 24 (found using Select len(max(Upline))FROM [source table]).
Any help would be appreciated. Thanks.
I found a work around. In the create statement of the destination table I changed varbinary(1000) to be varbinary(max) and, at the end, I replaced this ending:
WITH
(
DISTRIBUTION = ROUND_ROBIN,
CLUSTERED COLUMNSTORE INDEX
)
GO
with this:
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
GO
Now, the whole create statement looks like this:
CREATE TABLE [Common].[dimEmployee_temp]
(
[EmployeeSK] [int] IDENTITY(1,1) NOT NULL,
[DayForceEmployeeNumber] [nvarchar](255) NOT NULL,
[ConaEmployeeNumber] [char](10) NULL,
[EmployeeLastName] [nvarchar](255) NULL,
[EmployeeFirstName] [nvarchar](255) NULL,
[EmploymentStatus] [nvarchar](255) NULL,
[HireDate] [date] NULL,
[OriginalHireDate] [date] NULL,
[TerminationDate] [date] NULL,
[CurrentPosition] [nvarchar](255) NULL,
[PreviousPosition] [nvarchar](255) NULL,
[WorkAssignmentEffectiveStart] [date] NULL,
[Region] [nvarchar](255) NULL,
[District] [nvarchar](255) NULL,
[Site] [nvarchar](255) NULL,
[OnSiteDepartment] [nvarchar](255) NULL,
[DepartmentName] [nvarchar](255) NULL,
[UnionName] [nvarchar](255) NULL,
[ManagerDayForceEmployeeNumber] [nvarchar](255) NULL,
[Upline] [varbinary](max) NULL,
[Lvl] [int] NULL,
[dimStartDate] [date] NOT NULL,
[dimEndDate] [date] NULL,
[dimIsCurrent] [int] NOT NULL,
[dimHash] [nvarchar](256) NULL
)
WITH
(
DISTRIBUTION = ROUND_ROBIN,
HEAP
)
GO
This doesn't help me understand what is going on in the Azure data flow, but it does run successfully and insert into the table.

T-SQL: Conversion failed when converting date and/or time from character string

Below is a trigger used to capture updates/inserts on an SQL table. I cannot figure out why, but whenever an update is done, I get the error message Conversion failed when converting date and/or time from character string. Here is the structure of the Transaction Log table:
CREATE TABLE [dbo].[TransactionLog](
[Id] [int] IDENTITY(1,1) NOT NULL,
[TransactionDate] [datetime] NOT NULL,
[Operator] [varchar](35) NOT NULL,
[TableName] [varchar](50) NOT NULL,
[Action] [char](1) NOT NULL,
[TableString] [nvarchar](255) NOT NULL,
[UserId] [char](6) NULL,
CONSTRAINT [PK_TransactionLog] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
Here is the table being updated:
CREATE TABLE [dbo].[AgentContEd](
[Id] [int] IDENTITY(1,1) NOT NULL,
[sNumber] [int] NOT NULL,
[StateCode] [char](3) NOT NULL,
[CourseCode] [char](6) NOT NULL,
[DateTaken] [date] NOT NULL,
[ExpirationDate] [date] NULL,
[CourseHours] [smallint] NOT NULL,
[Method] [varchar](15) NULL,
[LastChangeOperator] [char](8) NOT NULL,
[LastChangeDate] [datetime] NOT NULL,
[ControlId] [int] NULL,
CONSTRAINT [PK_AgentContEd] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
And here is the trigger that's causing the headache...
BEGIN
INSERT INTO dbo.TransactionLog
(
TransactionDate,
Operator,
TableName,
Action,
TableString,
UserId
)
SELECT
LastChangeDate,
'Op',
#tableName,
#action,
CAST(
'ID:' + CAST(ISNULL(Id, 'NULL') as char(4))
+ ' SymNum:' + CAST(ISNULL(sNumber, 'NULL') as char(10))
+ ' StateCode:' + ISNULL(StateCode, 'NULL')
+ ' DateTaken:' + CAST(ISNULL(DateTaken, 'NULL') as nvarchar(9))
+ ' ExpDate:' + CAST(ISNULL(ExpirationDate, 'NULL') as nvarchar(9))
+ ' CourseCode:' + ISNULL(CourseCode, 'NULL')
+ ' Hours:' + CAST(ISNULL(CourseHours, 'NULL') as char(3))
+ ' Mthd:' + ISNULL(Method, 'NULL')
As char(255)),
LastChangeOperator
FROM inserted
END
Try
+ ' DateTaken:' + ISNULL(CAST(DateTaken as varchar(9)), 'NULL')
+ ' ExpDate:' + ISNULL(CAST(ExpirationDate as varchar(9)), 'NULL')
I used varchar as it seems pointless to use nvarchar if you are going to be casting the string to char at the end anyway.
Also you probably need to use CONVERT with a style instead of CAST to store something useful. SELECT CAST(getdate() as nvarchar(9)) returns Sep 28 20 for me.
A list of formats is here

Multi-table query - get friend updates

SELECT M.msg_id, M.uid_fk, M.message, M.created,
U.fname, U.lname, M.uploads
FROM messages M, users_friends F, users U
WHERE M.uid_fk=F.friendID
and F.userID = '5'
and status='2'
Building a facebook-like wall and want to grab messages(updates) from friends.
The query above returns an empty set, even though I've made sure there are messages from user 5 in the table.
Schema:
CREATE TABLE IF NOT EXISTS `messages` (
`msg_id` int(11) NOT NULL AUTO_INCREMENT,
`message` varchar(200) CHARACTER SET utf8 DEFAULT NULL,
`uid_fk` int(11) DEFAULT NULL,
`ip` varchar(30) DEFAULT NULL,
`created` int(11) DEFAULT '1269249260',
`uploads` varchar(30) DEFAULT NULL,
PRIMARY KEY (`msg_id`),
KEY `uid_fk` (`uid_fk`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=263 ;
CREATE TABLE IF NOT EXISTS `users` (
`fname` varchar(15) NOT NULL,
`lname` varchar(15) NOT NULL,
`userID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(23) NOT NULL,
`email` varchar(50) NOT NULL,
`password` varchar(32) NOT NULL,
`DOB` date DEFAULT NULL,
`sex` varchar(1) DEFAULT NULL,
`about` text NOT NULL,
`location` varchar(20) DEFAULT NULL,
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`user_level` int(11) NOT NULL DEFAULT '0',
`profile_image` varchar(200) NOT NULL,
`profile_image_small` varchar(200) NOT NULL,
PRIMARY KEY (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
CREATE TABLE IF NOT EXISTS `users_friends` (
`userID` int(11) NOT NULL,
`friendID` int(11) NOT NULL,
`status` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`userID`,`friendID`),
KEY `fk_users_has_friends_users1` (`userID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
You are single quoting your INT values in the WHERE clause → '5' & '2'.
Also, try JOINs.
SELECT M.msg_id,
M.uid_fk,
M.message,
M.created,
U.fname,
U.lname,
M.uploads
FROM messages M
INNER JOIN users_friends F ON F.friendID = M.uid_fk
AND F.userID = 5
AND F.status = 2
INNER JOIN users U ON U.userID = F.friendID;

Creating composite foreign key constraint

I am trying to create a composite foreign key relationship/constraint. All tables are empty. I have this table:
CREATE TABLE [dbo].[ChemSampleValueTest](
[SampleNumber] [int] NOT NULL,
[ParameterID] [int] NOT NULL,
[Value] [numeric](18, 6) NOT NULL,
[Accuracy] [varchar](50) NULL,
[ResultGroupID] [int] NOT NULL,
[QAState] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_SampleValueTest] PRIMARY KEY CLUSTERED
(
[SampleNumber] ASC,
[ParameterID] ASC,
[ResultGroupID] ASC
)
) ON [PRIMARY]
and this table:
CREATE TABLE [dbo].[ChemSampleValueEventLinkTest](
[Event] [int] NOT NULL,
[SampleNumber] [int] NOT NULL,
[ResultGroupID] [int] NOT NULL,
[ParameterID] [int] NOT NULL,
[QAState] [nvarchar](32) NULL
) ON [PRIMARY]
and I want to link them like this:
alter table [ChemSampleValueEventLinkTest] add
constraint FK_ChemSampleValueEvent_ChemSampleValue_test
foreign key ([SampleNumber], [ResultGroupID], [ParameterID])
references ChemSampleValueTest ([SampleNumber], [ResultGroupID], [ParameterID])
As far as I can tell all column types are the same, but it keeps on saying
There are no primary or candidate keys in the referenced table
'ChemSampleValueTest' that match the referencing column list in the foreign key
'FK_ChemSampleValueEvent_ChemSampleValue_test'.
Where am I going wrong?
It looks like you need to have your FK/references list in the same order they appear in the PK definition.
This should work:
CREATE TABLE [dbo].[ChemSampleValueTest](
[SampleNumber] [int] NOT NULL,
[ParameterID] [int] NOT NULL,
[Value] [numeric](18, 6) NOT NULL,
[Accuracy] [varchar](50) NULL,
[ResultGroupID] [int] NOT NULL,
[QAState] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_SampleValueTest] PRIMARY KEY CLUSTERED
(
[SampleNumber] ASC,
[ParameterID] ASC,
[ResultGroupID] ASC
)
) ON [PRIMARY]
CREATE TABLE [dbo].[ChemSampleValueEventLinkTest](
[Event] [int] NOT NULL,
[SampleNumber] [int] NOT NULL,
[ResultGroupID] [int] NOT NULL,
[ParameterID] [int] NOT NULL,
[QAState] [nvarchar](32) NULL
) ON [PRIMARY]
alter table [ChemSampleValueEventLinkTest] add
constraint FK_ChemSampleValueEvent_ChemSampleValue_test
foreign key ([SampleNumber], [ParameterID], [ResultGroupID])
references ChemSampleValueTest ([SampleNumber], [ParameterID], [ResultGroupID])