In the Azure SQL Database Management portal I was trying to add a new column to an existing table that contains rows.
I added the default value - 0 - thinking that it would add the default to existing rows and therefore setting the column to 'Is Required' (not nullable) would be OK. However, when I tried to save, I got the error:
ALTER TABLE only allows columns to be added that can contain nulls, or
have a DEFAULT definition specified, or ...
I could however execute the following SQL statement:
ALTER TABLE Sales
ADD Purchase int NOT NULL
CONSTRAINT PurchaseDefault DEFAULT 0
When I looked at the table again in azure, I saw that the Default value had ((0))
So, on the next column that I had to add I tried putting the default in double brackets ((0)), but that did not work either.
So, I went to SQL Server Management Studio 2012 on my dev machine. Here I can add a new column with 0 in the 'Default Value Or Binding' option. This worked. Interestingly, when I went back the 0 was now ((0)).
So, my questions are:-
How is the Default Value column in Azure different from adding a default constraint?
Why is this OK in SQL Server 2012, but not Azure?
What are the (()) about? I tried googling for various combinations of 'double brackets/braces default constraints', but came up empty.
I think what you met with were correct. I'm not a DBA but let me try to explain something I know for now.
If you are using SQL Server Management Studio (SSMS) to specify default value, it will be wrapped by brackets automatically. If your default value is a static value, for example zero, it will be ((0)), if it's some calculation value for example the current date/time, it will be (GETDATE()). But I'm not pretty sure why it must be wrapped by (double) brackets
I guess there are some existing records in your SQL Azure. Your understanding of ... it would add the default to existing rows ... is not correct. Default value will NOT affect existing records. It will just set the value to default if you don't specify explicitly. This is the reason why your got error when you applied your changes in SQL Azure. I think you need to run a script to set the default value to this columns for all existing records then apply this change. Something like UPDATE [Your_Table] SET [Purchase] = 0 WHERE [Purchase] IS NULL.
Hope this helps
Related
I just took over a project from a developer who has already left an organisation and I'm doing some maintenance work in the project. I can see the following code in a custom web part
CurrentUserInfo CurrentUser = MembershipContext.AuthenticatedUser;
DateTime ExpirationDate = CurrentUser.GetValue("aps_expirationdate", DateTime.Now);
The strange thing is, I cannot seem to find this custom field 'aps_expirationdate' anywhere in the system or in the database.
I checked the following places but couldn't find it.
Checked Membership module 'User' class and 'User - Settings' class
Checked 'User' module
Did a manual Sql Search in the database to find a table with a column name 'aps_expirationdate'
but I cannot seem to find this column anywhere and the other strange thing is, when I debug the code it does return a date value. No bugs in the code so, cannot say that this is an invalid column name. Where else should I look?
Based on what you've put in your initial question, it sounds as if the column was added manually and not through the Kentico UI. In order for the data access layer or the Kentico API to know that field exists, the definition has to be stored within the Kentico module class and not just in the database.
Here's what I'd do to correct this:
Find what table the field exists in. If you want the custom field to be part of the User or User Settings objects then they need to be added to either the CMS_User or CMS_UserSetting table.
If the field does not exist in either the CMS_User or CMS_UserSetting table, then go to the Modules app in the Kentico UI.
In the Modules App, go to Membership > Classes > Users > Fields and add the aps_expirationdate field.
If you have data in the field aps_expirationdate in the other table, write a query to copy it from the other table to the CMS_User table.
Now the API call as noted above will work.
If the field is already in the CMS_User or CMS_UserSetting table, then you will have to do the following:
In SSMS, rename that field to aps_expirationdate_old.
In the Modules App, go to Membership > Classes > Users > Fields and add the aps_expirationdate field.
Assuming you have data in the aps_expirationdate_old field in the CMS_User or CMS_UserSetting table, write a query to copy it from the other table to the CMS_User table.
Your last bullet point states you "Did a manual Sql Search in the database to find a table with a column name 'aps_expirationdate'". This contradicts what you state at the end of your question which states "I cannot seem to find this column anywhere". If you cannot find what table the aps_expirationdate exists in, then check out the following SO answer to find that column in a given database.
https://stackoverflow.com/a/4849704/698678
I've created some temporal tables in SQL Azure but I can't figure out how to modify them. What is the best approach to adding new columns or modifying existing ones?
MSDN has a lot of examples,In earlier versions of CTP,you cant alter Temporal table with out setting system_versioning to off.But starting with CTP3,you can do this...Here are few examples..
ALTER TABLE dbo.Department
ALTER COLUMN DeptName varchar(100);
ALTER TABLE dbo.Department
ADD WebAddress nvarchar(255) NOT NULL
CONSTRAINT DF_WebAddress DEFAULT 'www.mycompany.com';
ALTER TABLE dbo.Department
ADD TempColumn INT;
GO
ALTER TABLE dbo.Department
DROP COLUMN TempColumn;
/* Setting IsHidden property for period columns.
Use ALTER COLUMN <period_column> DROP HIDDEN to clear IsHidden flag */
ALTER TABLE dbo.Department
ALTER COLUMN SysStartTime ADD HIDDEN;
ALTER TABLE dbo.Department
ALTER COLUMN SysEndTime ADD HIDDEN;
MSDN also recommends to do any scheme change in a transaction like below..
BEGIN TRAN
---set system versioning to off
ALTER TABLE [dbo].[CompanyLocation] SET (SYSTEM_VERSIONING = OFF);
ALTER TABLE [CompanyLocation] ADD Cntr INT IDENTITY (1,1);
ALTER TABLE [dbo].[CompanyLocationHistory] ADD Cntr INT NOT NULL DEFAULT 0;
--specifying history table is needed with out which ,SQL adds a new history table
ALTER TABLE [dbo].[CompanyLocation]
SET
(
SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[CompanyLocationHistory])
);
COMMIT ;
You may wonder why we need to set system versioning off ,as i said above it can be done with out it starting with ctp3.0,this is because of few limitations..
You cannot use direct ALTER for the following schema changes. For these types of changes, set SYSTEM_VERSIONING = OFF.
Adding a computed column
Adding an IDENTITY column
Adding a SPARSE column or changing existing column to be SPARSEwhen
the history table is set to DATA_COMPRESSION = PAGE or
DATA_COMPRESSION = ROW, which is the default for the history table.
Adding a COLUMN_SET
Adding a ROWGUIDCOL column or changing existing column to be
ROWGUIDCOL
There are two ways to alter temporal tables:
Via Transact-SQL script. For more details see https://msdn.microsoft.com/en-us/library/mt591016.aspx
Using SQL Server Data Tools, that integrates with Visual Studio. It's convenient if you need to version your database scripts in version control system (TFS for instance) : https://msdn.microsoft.com/en-us/library/mt204009.aspx
Thanks,
Borko Novakovic (MSFT)
Via scripts.
ALTER TABLE ...
The world is slowly coming to grips with DevOps, and how graphical user interfaces for dealing with quick changes aren't really helping.
Every alteration script and modification should be stored in version control, and executed for the modern, cloud-based reality, which means basically that what we could do with a simple designer before, is now something we usually write as a script that can be applied to a cluster of databases, for rebuilding the test environment etc.
I know it's not the funniest answer, but being prepared for this is vital to your future skillset.
I am using the Logic App designer on Azure. Added "SQL Azure - Insert Row" as an Action, it prompts me to insert values for the columns but all the columns are marked as required and it doesn't save the changes/sequence if a value is not entered there.
Is there anyway of bypassing this or making the columns optional?
I tried this previously, there was no way to make the columns optional but i figured a workaround.
First, in the designer put any value, then open Code view and look for the columns you don't need and just delete it from the JSON schema and hit save :)
that should do it for you, but next time you open in designer and try to edit you have to do this again.
let me know if it helps
Edited
I added a screenshot of the steps that i made while trying to replicate this. (note that your database schema should handle these null values for the columns, in my case the Guid had a default value of newid() and the other column allowed nulls). You always pass null to the columns, not passing values at all might make sense for columns that has values computed or has default values (like newid() uniqueidentifier or getdate() for created/modified time)
I have a report that is up and running but I need to restrict the data on a parameter that takes multiple string-valued staff codes.
I created a parameter called Practioner which is a text type, ticked on Allow Multiple values and also on Visible.
In the available values, I made this code come from the field called mbillaty from the dataset3 under the Get values from a query option. I've checked data3 is fine in SQL server.
3.Now, I've gone to my dataset1 where all the reporting data sits then put the parameter, with the name ? and =Join(Parameters!Practitioner.Value,", ") for the parameter value. Then in the query, in the where clause, I've put: ......mbility in (?)
What should this SSRS be interpreting as is : .......mbility in ('AAB','KKR','RDR'), if the user picked these three staffs.
When I run the report, it definitley works when I check one of the practioners, but as soon as I more than one, the report wouldn't run. It does not return any error, but just the header shows, which I think means that no data is found.
Experts, do you see where I've gone wrong with this?
Thanks
If you're able to create UDFs, this link might be helpful. How do I split a string so I can access item x?
The parameter would be parsed in the SQL/function, and SSRS would simple pass the concatenated string.
I have a table with a PrimaryKey that is set as uniqueidentifier (GUID) and it's autopopulated using the newsequentialid() function from sql server 2008... When I insert a row manually, everything works as normal. But when I insert the record using the subsonic class and the repository the GUID column defaults to all zero's and the database will enter that value. I need it to NOT do this and use the default value of the column... anyone else able to get around this issue?
This was fixed in 2.0.2 for SQL 2005 - I'm wondering if there's an issue with SQL 2008. What version are you using? I'm thinking this might not have been covered in 3.x just yet.