I am facing an problem to set an default value in sql select query
ex: t1.Employee as EmpDeptName
here i need to set an default value so that it returns always the default value
what i tried t1.Employee as EmpDeptName 'ITdept'
But this fails always telling incorrect syntax
If you want to SELECT this value when you call a query,
SELECT 'ITdept' AS EmpDeptName, ...
If you want to use "ITdept" as the default value when the column is empty,
SELECT ISNULL(EmpDeptName, 'ITdept') AS EmpDeptName, ...
If you want to set the column default,
CREATE TABLE t1 (
EmpDeptName <data type> DEFAULT 'ITdept'
);
Related
This DDL:
CREATE TABLE example (
my_stamp TIMESTAMP_NTZ NOT NULL DEFAULT CURRENT_TIMESTAMP(0)
)
Yields this error:
SQL compilation error: Default value data type does not match data type for column MY_STAMP
Changing CURRENT_TIMESTAMP(0) to CURRENT_TIMESTAMP makes the error "go away".
Yet, this command returns successfully:
select CURRENT_TIMESTAMP(0), CURRENT_TIMESTAMP;
This would also work, I believe. You just need to cast the output of the function to the same timestamp data type as the my_stamp column:
CREATE TABLE example (
my_stamp TIMESTAMP_NTZ NOT NULL DEFAULT CURRENT_TIMESTAMP(0)::TIMESTAMP_NTZ
)
I am attempting what I thought was a relatively easy thing. I use a SQL task to look for a filename in a table. If it exists, do something, if not, do nothing.
Here is my setup in SSIS:
My SQL Statement in 'File Exists in Table' is as follows, and ResultSet is 'Single Row':
SELECT ISNULL(id,0) as id FROM PORG_Files WHERE filename = ?
My Constraint is:
When I run it, there are no files in the table yet, so it should return nothing. I've tried ISNULL and COALESCE to set a value. I receive the following error:
Error: 0xC002F309 at File Exist in Table, Execute SQL Task: An error occurred while assigning a value to variable "id": "Single Row result set is specified, but no rows were returned.".
Not sure how to fix this. The ISNULL and COALESCE are the things suggestions found in SO and on MSDN
Try changing your SQL statement to a COUNT then your comparison expression would read #ID > 0. So, if you have files that match your pattern the count will be greater than 0 if there are no files it will return 0.
SELECT COUNT(id) as id FROM PORG_Files WHERE filename = ?
If you want to check if a row exists then you should use Count as #jradich1234 mentioned.
SELECT COUNT(*) as id FROM PORG_Files WHERE filename = ?
If you are looking to check if a row exists and to store the id in a variable to use it later in the package, first you have to use TOP 1 since you selecting a single row result set and you can use a similar logic:
DECLARE #Filename VARCHAR(4000) = ?
IF EXISTS(SELECT 1 FROM PORG_Files WHERE filename = #Filename)
SELECT TOP 1 id FROM PORG_Files WHERE filename = #Filename
ELSE
SELECT 0 as id
Then if id = 0 then no rows exists.
It just needs to use UNION another row empty
always return a value
Like it if work for you
SELECT foo As nameA, fa AS nameB
UNION ALL
SELECT NULL AS nameA, NULL AS nameB
And then you can validated (line) contrained if var is null no allow
I have this query that is supposed to get all results where active=TRUE in table, but i want to ensure if users lets say changed the active value in the Table to say "True" or "tRue" that the query recognizes it as intended TRUE, by somehow applying Uppercase ON the query condition all the time
$Table = Query "SELECT * from [dbo].[$cubeTable] WHERE [active] = 'TRUE'.ToUpper()"
write-host $Table += $row.Item("active")
Notice, this is what I have but of course, it throws error
WHERE [active] = 'TRUE'.ToUpper()"
Exception calling "Fill" with "1" argument(s): "Cannot call methods on varchar."
Default collation will provide the functionality you require (case-insensitive match) by default, however if you want to force an insensitive match if your collation is case sensitive then you can use UPPER function but be aware this will cause a full scan of your table (and can therefore have some major performance implications).
To check your current collation:
SELECT DATABASEPROPERTYEX('DbName', 'Collation') SQLCollation;
This will give you the collation, example Latin1_General_CI_AS
Here CI means Case Insensitive
If you have this then you are good to go. If not you could do:
SELECT * from [dbo].[$cubeTable] WHERE UPPER([active]) = 'TRUE'
But this will scan your whole table.
If you have the ability to change your schema you could force the collation for the column if you want:
CREATE TABLE [#CollationTest]
(
[MyColumnName] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS
)
INSERT INTO [#CollationTest] ([MyColumnName]) VALUES ('Value123')
INSERT INTO [#CollationTest] ([MyColumnName]) VALUES ('value123')
SELECT * FROM [#CollationTest] WHERE [MyColumnName] = 'value123' -- Returns 2 rows
DROP TABLE [#CollationTest]
I have created a not nullable user defined data type using the below script
CREATE TYPE [dbo].[booleano]
FROM TINYINT NOT NULL;
EXECUTE sp_bindefault #defname = N'[dbo].[cero]', #objname = N'[dbo].[booleano]';
It has an bind default value, which is 0.
When I try to add a new column to a table, and this is a not null column it return the following error
ALTER TABLE [dbo].[con_impresion]
ADD [cim_imprime_monto_letras_boo] [dbo].[booleano] NOT NULL;
Rows were detected. The schema update is terminating because data loss might occur.
But if add a default constraint to the new column it returns the following error
ALTER TABLE [dbo].[con_impresion]
ADD [cim_imprime_monto_letras_boo] [dbo].[booleano] DEFAULT 0 NOT NULL;
Column already has a DEFAULT bound to it.
I am trying to use XQuery in SQL Server 2005 to update xml saved in a column. Here is a sample of the data I need to update.
<Length>3</Length>
<Width>5</Width>
<Depth>6</Depth>
<Area xsi:nil="true" />
<Volume xsi:nil="true" />
I need to set the area and volume to values from a different table. I am creating a CTE for the update. There is other logic that I have omitted, but I have verified that the CTE contains the correct data for the update:
;with Volume (DocumentID, Volume) As
(
Select DocumentID, Volume from tbl
)
and I am using the following XQuery SQL statement to try to update the table.
UPDATE tbl_Archive
SET XML.modify(' declare namespace x="http://www.redacted.com";
replace value of (/x:Document/x:Volume/text())[1]
with sql:column("Volume.Volume")')
From Volume where volume.documentID = tbl_Archive.DocumentID
I get 1 row affected, but when I look at the XML it hasn't changed, and I can't figure out what needs to be fixed to make it work. The node is untyped, if that makes any difference.
Update wont work if there's no text to replace.. the XPath /x:Document/x:Volume/text())[1] will return an empty set.
Try insert...
UPDATE tbl_Archive
SET XML.modify(' declare namespace x="http://www.redacted.com";
insert text {sql:column("Volume.Volume")}
as first into (/x:Document/x:Volume)[1]')
From Volume where volume.documentID = tbl_Archive.DocumentID
..you'll then need to remove the nil="true" attribute..
Something like this maybe..
update tbl_Archive set XML.modify('delete /*:Document/*:Volume[text()]/#xsi:nil')