It is possible to move a table from one schema to another:
ALTER TABLE my_table SET SCHEMA different_schema;
However, I cannot find the equivalent feature for moving a function from one schema to another.
How can I do this?
(version 8.3+)
Taken from the docs:
ALTER FUNCTION name ( [ [ argmode ] [ argname ] argtype [, ...] ] )
SET SCHEMA new_schema
Related
I can't find example how to add JSONB column to PostgreSQL.
I tried: ALTER TABLE available_routes ADD COLUMN accounts TYPE JSONB;
But got error about wrong syntax near "JSONB"
try:
ALTER TABLE available_routes ADD COLUMN accounts JSONB;
https://www.postgresql.org/docs/current/static/sql-altertable.html
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE
collation ] [ column_constraint [ ... ] ]
column_name data_type - with no type between
New to SQL, working on a databases project. I have two custom types, changedat and changedby defined by the following code:
CREATE TYPE [dbo].[changedat] FROM [smalldatetime] NULL
GO
CREATE TYPE [dbo].[changedby] FROM [nvarchar](30) NULL
GO
I'm working on my rollback script and not sure how to drop these types. They don't show up in sys.objects.
DROP TYPE dbo.changedat;
DROP TYPE dbo.changedby;
GO;
Look at the sql-server doc there you can see that you have ro use
DROP TYPE [ schema_name. ] type_name [ ; ]
I was wondering why this simple sql function won't work?
CREATE FUNCTION dbo.getTableCounts (#STREAM_ID nvarchar(10))
RETURNS table
AS RETURN ( select 1 as 'one', 2 as 'two');
When I run it in qwerybuilder I get two error messages:
Incorrect syntax near the keyword 'table' (for line 2)
and
Incorrect syntax near ',' (for line 3)
I don't understand how this is incorrect syntax. Does anyone see why this is an error?
According to ASE's Reference manual You can only return a scalar expression:
create function [ owner_name. ] function_name
[ ( #parameter_name [as] parameter_datatype [ = default ]
[ ,...n ] ) ]
returns return_datatype
[ with recompile]
as
[begin]
function_body
return scalar_expression
[end]
So you can't use table as data type.
Unlike Microsoft SQL Server, in Sybase if you need table as a result type you don't use a function but a stored procedure. In your case the definition of the procedure (written in Watcom-SQL) will look like:
CREATE PROCEDURE getTableCounts(#STREAM_ID NVARCHAR(10))
RESULT(one INT, two INT)
BEGIN
SELECT 1 AS one, 2 AS two;
END;
However this option is not supported by Transact-SQL dialect in Sybase.
I want to create a function in SQL Server 2005 that returns a table which the query is passing from my program...
But when I create that function with this script:
CREATE FUNCTION fn_test (#source varchar(255))
RETURNS TABLE AS
RETURN
EXECUTE (#source)
The script is showing error message
Incorrect syntax near the keyword "EXECUTE`
It's perfectly correct - you're not allowed to execute arbitrary SQL as part of an inline table valued function:
--Transact-SQL Inline Table-Valued Function Syntax
CREATE FUNCTION [ schema_name. ] function_name
( [ { #parameter_name [ AS ] [ type_schema_name. ] parameter_data_type
[ = default ] [ READONLY ] }
[ ,...n ]
]
)
RETURNS TABLE
[ WITH <function_option> [ ,...n ] ]
[ AS ]
RETURN [ ( ] select_stmt [ ) ]
[ ; ]
If you need(*) to have a facility to pass arbitrary SQL into a SQL Server object and have it execute it, use a stored procedure rather than a function. Functions are not meant to alter the state of the database, but arbitrary SQL can do... arbitrary things.
(*)You don't.
As a procedure, it would be:
CREATE PROCEDURE test (#source varchar(255))
AS
EXECUTE (#source)
But as is probably evident at this point - if you want to execute arbitrary SQL stored as strings, you may as well just directly call EXECUTE on them. That's part of what I was alluding to when I put my (*) in. The other part is - why send it in a string variable to the server at all - why not just send the SQL you want to execute, if you're going to run arbitrary SQL on the server anyway.
Here is the create statement:
create table dbmonitor.DBMON_DATABASE_TYPE (
DATABASE_TYPE_ID BIGINT IDENTITY NOT NULL,
DispName NVARCHAR(255) null,
primary key (DATABASE_TYPE_ID)
)
and this is the error I get:
13:40:57,685 ERROR [TestRunnerThread] SchemaExport [(null)]- The table name is not valid. [ Token line number (if known) = 1,Token line offset (if known) = 24,Table name = DBMON_DATABASE_TYPE ]
The table name is not valid. [ Token line number (if known) = 1,Token line offset (if known) = 24,Table name = DBMON_DATABASE_TYPE ]
Possibilities:
Is dbmonitor the name of your database? You can't put a . in a table name.
Do you mean CREATE TABLE dbmonitor.dbo.DBMON_DATABASE_TYPE?
Did you try CREATE TABLE DBMON_DATABASE_TYPE?
I'm not sure if dbmonitor is meant to be a schema name, but according to the documentation for the SQL CE CREATE TABLE statement, you cannot include a schema name with the table name.
Contrast this for SQL Server 2005 Compact Edition (just showing the initial part of the statement),
CREATE TABLE table_name
( { < column_definition > | < table_constraint > } [ ,...n ]
)
with this for SQL Server 2008:
CREATE TABLE
[ database_name . [ schema_name ] . | schema_name . ] table_name
This may not be exactly an answer for this question's criteria , but for those who might get here :
this error can also happen when you try to use EntityFramework.Extended library with Sql Server CE. It seems that they are not compatible.
check these links :
https://github.com/loresoft/EntityFramework.Extended/issues/35
https://github.com/loresoft/EntityFramework.Extended/issues/11