April 14, 2021 at 4:54 pm
Hello Community,
I trying to use the following sql code to change the Schema for all my tables from dbo to Config
SELECT 'ALTER SCHEMA Config TRANSFER [' + SysSchemas.Name + '].[' + DbObjects.Name + '];'
FROM sys.Objects DbObjects
INNER JOIN sys.Schemas SysSchemas ON DbObjects.schema_id = SysSchemas.schema_id
WHERE SysSchemas.Name = 'dbo'
AND (DbObjects.Type IN ('U', 'P', 'V'))
But the schema remains the same.
Can someone let me know where I'm going wrong?
April 14, 2021 at 4:59 pm
Your code is merely returning results. If you want to run the code, you need to paste those results into SSMS and hit F5 ... very carefully and having backed up your DB, I'd suggest.
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 14, 2021 at 5:04 pm
Hi Phil,
Thanks for getting in touch,
I pasted the code into SSMS but the schema remains the same
April 14, 2021 at 5:07 pm
Sorry, I'm being an idiot.
I'm getting the following error
Msg 15151, Level 16, State 1, Line 1
Cannot alter the schema 'Config', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Line 2
Cannot alter the schema 'Config', because it does not exist or you do not have permission.
April 14, 2021 at 5:11 pm
Sorry, I'm being an idiot.
I'm getting the following error
Msg 15151, Level 16, State 1, Line 1 Cannot alter the schema 'Config', because it does not exist or you do not have permission. Msg 15151, Level 16, State 1, Line 2 Cannot alter the schema 'Config', because it does not exist or you do not have permission.
OK, that's a pretty clear error message!
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
April 14, 2021 at 5:30 pm
Issue this command first:
CREATE SCHEMA config;
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 14, 2021 at 6:52 pm
Hi Phil,
I just tried changing the schema with the username that was used to actually create the database but I'm still getting the error
Cannot alter the schema 'Config', because it does not exist or you do not have permission.
Can you let me know how to set the permissions to alter the schema please?
April 14, 2021 at 7:12 pm
Don't worry.
I realised the problem was that I didn't have a schema called called Config in the first place - doh!
April 15, 2021 at 10:48 am
Side note, syntax like '[' + {Object Name} + ']'
isn't safe from injection. If you had an object with the ]
character in it's name (yes, there are people who are silly enough to do that) you would get a syntax error, and of course if it was a user parameter then someone malicious would easily be able to escape the string. You should be using QUOTENAME
to properly quote the dynamic object's name instead.
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
April 15, 2021 at 12:25 pm
Don't worry.
I realised the problem was that I didn't have a schema called called Config in the first place - doh!
I'm going to assume that wine or beer were involved 🙂
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply