SQL Server query (create rule) - sql-server

Create a rule called Makedata to allow only the following values to the make of the data: txt, excel, word, rar, and powerpoint.
You must attach the rule to column Make in the datatype table
What does this question mean? I don't want a solution just an explanation.
Thank you

You are asked to filter the values that can be stored in that data column. For this you can use enum data type if were using MySQL. To fit your case take a look at: SQL Server equivalent to MySQL enum data type?
Alter your table with:
column_name VARCHAR(255) NOT NULL CHECK (column_name IN('txt', 'excel', 'word', 'rar', 'powerpoint'))

Related

What is the equivalent datatype for SQL Server hierarchyid in PostgreSQL

I have a table in SQL Server which has a column of type hierarchyid, the issue is that I can't find the equivalent datatype for this in PostgreSQL.
CREATE TABLE dbo.exampleTable{
id int;
name varchar(255);
level hierarchyid not null;
}
How would I write the equivalent in PostgreSQL?
There is no a direct equivalent datatype in PostgreSQL and without more information on your use case, as Tim correctly says in his comment it is difficult to provide a full answer.
You do have 2 options:
You may want to look at the ltree module in the documentation:
[https://www.postgresql.org/docs/13/ltree.html][1]
Another way hierarchical data is modelled in PostgreSQL is using
materialized views with recursive common table expressions.

SQL Server syntax for NEWID()

I need some help with some SQL Server 2014 syntax.
I want to use the NEWID() function to create the UUID value in column CareplannersUUID. Each time a record is inserted, I want a new UUID value to appear in column CareplannersUUID.
But I am not sure about the syntax of the command. I think it is something like:
ALTER TABLE CareplannersMembers
ADD CONSTRAINT DF_table_id DEFAULT (NEWID()) FOR CareplannersUUID
I am wondering specifically, is there another value I should add for DF_table_id?
Notes about the table in question:
Table name: CareplannersMembers
UUID column name: CareplannersUUID
Thank you for your help.
Eric
I looked at the similar questions, but am not able to determine, from information presented there, whether or not I am using the correct syntax for my SQL Server statement.

how to convert varchar dataype to text in mssql database?

i have table with data and now i want to change its column dataype from varchar to text,
ALTER TABLE ver_table ALTER COLUMN field text;
unfortunately it gives the
SQL Error [1088] [S1000]: Cannot find the object "ver_table" because it does not exist or you do not have permissions.
Cannot find the object "ver_table" because it does not exist or you do not have permissions.
but the table actualy exists with data.
The query should support in all versions of Sql server.
Is any other to achieve this without losing of data, i mean any procedure ?
Please do correct me.
thanks
You should avoid using the text data type.
Important
ntext, text, and image data types will be removed in a
future version of Microsoft SQL Server. Avoid using these data types
in new development work, and plan to modify applications that
currently use them. Use nvarchar(max), varchar(max), and
varbinary(max) instead.
Source: Microsoft Docs.

Inserting arabic characters into sql server database from grails

I have a grails application that is intended to insert arabic characters from interface to sql server database. First I did it with field type varchar but it appears ???????. Then I found that the field type should be nvarchar but with, application simply fails to insert any row by giving the following error,
java.sql.SQLException: Cannot insert the value NULL into column 'id', table 'smsCampaigner.dbo.message'; column does not allow nulls. INSERT fails.
I know that this is constraints error that id cannot be null but this was not the case before changing the field type. Now I want to know whether we can specify fro config.groovy which encoding should be used in database ? any other type of help will be appreciated.
here you go..
when you create the domain class use String type for that column and that's it. you could insert any character it will accept.
for example TestDomain.groovy
class TestDomain {
String column1
}
In mysql you can set up the collation of the database/table/column to use utf8_unicode_ci and if I am not wrong that is valid to save arabic chars. Not sure how to do it with SQL Server but it should be something similar.
Change the collation of the database and tables and columns to Arabic_CI_AS
In Sql Server management studio (SSMS), Execute :
ALTER DATABASE DB06 COLLATE Arabic_CI_AS
where DB06 is the name of the database.
right-click on table, then click design, then select the desired column, in its properties, choose the collation to Arabic_CI_AS
by the way, the error :
Cannot insert the value NULL into column
is not related to Arabic, this is because you are trying to insert NULL in the column that does not allow NULL.

How to use SQL's LIKE on a guid in Entity Framework?

In SQL Server, uniqueidentifier columns can be used like strings with the LIKE comparison:
SELECT * FROM dbo.Table WHERE GuidColumn LIKE '4c38e01e%'
How can I use this feature in Entity Framework?
I want something like this
context.Table.Where(x => x.StringColumn.Contains("some string"))
but for a Guid column instead for string columns:
context.Table.Where(x => x.GuidColumn ???? "4c38e01e")
As far as I know there is no way to convert Guid to string on the fly in EntityFramework.
I'm aware of some workaraounds but they are not elegant to me though they may be helpful in your case:
Create a view in SQL server where you convert your Guid column to string. Then create a new entity and map it to the view. Now you have your guid column in a string represantation and can apply 'like' operator. The main drawback is query performance.
Create a PERSISTENT calculated column of string type. Use convert expression to convert your Guid to string. Query performance would be the same as for an ordinary string column, but it will take a bit longer to insert records into your table.
If this is not the only filter applied to your data source then maybe you can apply your GUID LIKE operation when the data is already in memory.
Hope it helps.
I worked around this by creating a stored procedure in the database that handled the 'like' clause in the database, and mapping the procedure results to my entity from the edmx. So my stored proc is:
CREATE PROCEDURE [dbo].[spGetDevicesWhereGuidContains]
#partialGuidString as varchar (5)
AS
BEGIN
SET NOCOUNT ON;
SELECT * from devices where [Guid] like '%' + #partialGuidString + '%'
END
Then, mapping the proc to the entity from the model browser:
http://entityframeworktutorial.net/data-read-using-stored-procedure.aspx#.UT0F96GG1e4
Then it's obvious from there;
List<Device> devices = context.spGetDevicesWhereGuidContains("8").ToList();
Not ideal, but does the job.

Resources