how to convert varchar dataype to text in mssql database? - sql-server

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.

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 query (create rule)

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'))

SQL Server 2012 missing RowVersion datatype

I recently restored my database onto a new SQL Server 2012 and notice that all my table columns of RowVersion data types have been changed to binary(8)
In Management Studio when I design the tables, the drop down for the data types no longer even has a choice for rowversion.
I cant find anything from MS other than old information that says it should still exist, or that it is equivalent to binary(8) however binary(8) doesn't auto generate a value like rowversion or timestamp.
Thanks
I haven't actually tried this but it should be something like:
ALTER TABLE YourTable
ALTER COLUMN YourCol rowversion
I think it's by design. Conversion to binary(8) means that any values generated so far are preserved but in order to carry on auto-generating, you need to convert back to rowversion using the above.

SQL Server varchar(MAX) datatype in delphi using RemObjects

Got a request to change comment field max size in application. Before had it set to varchar(500), so after reading documentation i have decided to change data type of the field from varchar(500) to varchar(max). Database accepted changes without any problems (using Microsoft SQL Server Management Studio 2005 and Microsoft SQL Server Management Studio 2008 for database management).
Then i went on changing the software. Software is written in Delphi with RemObjects to communication with database. So I changed the TDASchema for the server, it mapped my new varchar(max) field as String(65536) data type (got me a little worried there about such an explicit static size, but I went on). Then I Retrieved DataTable Schema for my TDAMemDataTable object, which updated all the fields.
I started the application and decided to see whether my database will accept changes on this specific changed field. I have edited one of the records and clicked the button to synchronize the DataSet with server and got such a fail message:
The data types varchar(max) and text are incompatible in the equal to operator
I interpret it as that my server object (the one that maps database fields with RemObjects objects) have mapped field data types to wrong data types in RemObjects.
How can this be resolved? What are the alternatives?
P.S. In this release Build .1267 logs from RemObjects it clearly states that:
fixed: DataSnap: fails to post updates to MSSQL 2005 VARCHAR(MAX)
I am using build version .1067. Wonder if update will fix the problem
P.P.S. After update to the latest version of RemObjects, the problem persists.
This error message usually happens when trying to compare a varchar(n) and text using an equality operator (usually in a where clause in sql but possible elsewhere). there was an article on MSDN which covered a few points which might relate to this.
when you store data to a VARCHAR(N) column, the values are physically stored in the same way. But when you store it to a VARCHAR(MAX) column, behind the screen the data is handled as a TEXT value. So there is some additional processing needed when dealing with a VARCHAR(MAX) value. (only if the size exceeds 8000)
You mentioned that the TDASchema had mapped your new field as String(65536) which, although never having used RemObjects before, i would assume somewhere in it's own code (or yours) is trying to do a comparison of some kind hence the error message.
Try using VARCHAR(8000) instead of MAX and see if that fixes the issue.
The other option if you can find where in the code it is doing this equality check, is to try doing a cast()
As you suspected, I think the root of your problems is that the fields haven't come into the TDASchema as the correct types. I've just tried it here and varchar(max) and nvarchar(max) fields come through to my schema as Memo and WideMemo respectively, not String(65536).
I'm using Delphi XE6 and SQL Server 2008 R2 via FireDAC.
This suggests an issue retrieving the metadata from the database. What database driver are you using? Can you try FireDAC (if available) or another driver to see if the problem persists?
Resolution for Delphi 7 and MS SQL Server 2008 R2 (SP2)
Delphi:
with TADOStoredProc.Create(Self) do
try
Connection := AConnection;
ProcedureName := ASPName;
Parameters.Refresh;
Parameters.ParamByName('#XML').Value := AXML;
try
ExecProc;
...
MS SQL Server:
ALTER PROCEDURE dbo.StoredProcName
#XML NVARCHAR(MAX)
,#ErrMsgOut NVARCHAR(MAX) = NULL OUT
AS BEGIN
SET NOCOUNT ON
DECLARE #RETURN INT = 0
,#idoc INT
BEGIN TRY
-- Prepare XML
DECLARE #XML_TEXT VARCHAR(MAX)
SET #XML_TEXT = CONVERT(VARCHAR(MAX), #XML)
EXEC sp_xml_preparedocument #idoc OUTPUT, #XML_TEXT
-- Open XML
SELECT *
FROM OPENXML (#idoc, '/ServicesList/ServicesItem', 2)
WITH
(
YourFields AndTypes
)
...

how can i change a field in my SQL database from numeric(18) to varchar(10)

I have a zipcode field in a database that I just took over. Previously, it was set as a numeric field with 18 precision but I am trying to convert it over to varchar(10).
I need to make this change because the linq fields are coming in as decimal and are causing issues and i want to change the linq fields to simply be strings.
I tried this in SQL server enterprise manager but i get this error, saying:
that the table will have to be dropped and recreated. you have either made changes to a table that can't be recreated or enable the option to prevent saving changes that require a table recreation
Any suggestions?
to enable that option in SQL management studio uncheck the following option...
Tools / Options / Designers / Table and Database Designers / Prevent saving changes that require table re-creation
You could also run an alter statement to change your datatype (as long as all of your data will fit in a varchar(10) column).
ALTER TABLE MyTable
ALTER COLUMN MyZipCodeColumn VARCHAR(10)
Are you using MS-SQL 2008? Changes that require the table to rebuilt are blocked by default.
Click Tools->Options, then Designers. Uncheck "Prevent saving changes that require table re-creation".
Then you can change your column using the designer.
Screenshots on how to do it:
http://pragmaticworks.com/community/blogs/brianknight/archive/2008/06/04/sql-server-2008-designer-behavior-change-saving-changes-not-permitted.aspx

Resources